Ruby學習筆記 陣列的處理array

2021-06-21 03:39:41 字數 2376 閱讀 9514

arr=[4,5,6]

print arr.join(","),"\n" #4,5,6

#print arr.join(", "),"\n" 意思是:將陣列arr轉換成字串輸出,用", "隔開每個元素,並且換行。

arr[4]="m" #把4號索引為止元素賦值為"m"

print arr.join(","),"\n" #4,5,6,,m

print arr[3],"\n" #列印3號索引位置元素 #nil

arr.delete_at(3) #刪除3號索引位置元素

print arr.join(","),"\n" #4,5,6,m

arr[2]=["a","b","c"] #把2號索引位置元素賦值為["a","b","c"]

print arr.join(","),"\n" #4,5,a,b,c,m

arr[0..1]=[7,"h","b"] #把0..1號元素替換為7,"h","b"

print arr.join(","),"\n" #7,h,b,a,b,c,m

arr.push("b") #加入元素"b"

print arr.join(","),"\n" #7,h,b,a,b,c,m,b

arr.delete(["a","b","c"]) #刪除元素["a","b","c"]

print arr.join(","),"\n" #7,h,b,m,b

arr.delete("b") #刪除所有元素"b"

print arr.join(","),"\n" #7,h,m

arr.insert(3,"d") #在3號索引位置插入元素"d"

print arr.join(","),"\n" #7,h,m,d

arr<<'f'<<2 #加入元素"f";加入元素2

print arr.join(","),"\n" #7,h,m,d,f,2

arr.pop #刪除尾元素

print arr.join(","),"\n" #7,h,m,d,f,

arr.shift #刪除首元素

print arr.join(","),"\n" #h,m,d,f,

arr.clear #清空陣列arr

print arr.join(","),"\n" #

aaaa=["aa",4,5,"bb"]

bbbb=[4,1,3,2,5]

print aaaa+bbbb,"\n" #aa 4 5 bb 41325

print aaaa*2,"\n" #aa 45 bb aa 45 bb

print bbbb-aaaa,"\n" #132

#並運算;交運算

print aaaa|bbbb ,"\n" #aa 45 bb 132

print aaaa&bbbb , "\n" #45

#排序;倒置

print bbbb.sort.join(",") ,"\n" #1,2,3,4,5

print aaaa.reverse,"\n" #bb54aa

str1='this is str1'

str2="this is str2"

str3=%q/this is str3/

str4=%q/this is str4/

str5=<; ( ); [ ] ;;等等。

str5是乙個字串文件,從 《和文件結束符的下一行開始,直到遇到乙個放置在行首的文件結束符,結束整個字串文件。

str="this is you string."

puts str*2 #this is you string.this is you string.

str="this is you string.\n"

puts str*2 #this is you string.

#this is you string.

str="\tthis is you string."

puts str # this is you string.

str='this\'s you string.\n'

puts str #this\`s you string.\n

單引號括起來的字串並不會對字串作任何解釋,你看到的是什麼便是什

麼,有乙個例外:單引號字串裡的 單引號 需要轉義。

Scala學習筆記 陣列

對於scala來說,陣列是一塊相當總要的內容,因此掌握是十分重要的。陣列分為兩種 定長陣列 array 變長陣列 arraybuffer array 10個整數的陣列,所有元素的初始化為0 val nums new arraystring 長度為2的array string 型別是推斷出來的 注 已...

JS學習筆記 陣列

ecmas cript 中沒有提供類和介面等的定義,但它卻是一門物件導向的語言,由於它能夠通過其它 方式實現類似高階語言的物件導向功能,這些內容將在後面的文章中進行一步步的總結。此篇僅對 js中物件作簡要說明,重點彙總一些類似於物件的陣列的經常用法。物件建立 上篇提到建立物件的兩種方式 使用new ...

PHP學習筆記 陣列

學習的內容太多,用文章記錄知識點可以加深印象,也方便查詢!陣列初始化 php array array foo bar bar foo 自 php 5.4 起 array foo bar bar foo 初始化為空陣列 php array array array push array push arr...