參考鏈接1:
一、數組聲明
int arr[ ];
int[ ] arr2;
二、數組初始化
int arr[] = new int[]{1, 3, 5, 7, 9};
int[] arr2 = {2, 4, 6, 8, 10};
三、遍歷數組
遍歷數組可用for/foreach
foreach:
for (int x: arr) { System.out.print(x + "\t"); }
四、Arraysfill填充數組(修改數組)
void fill(int[] a, int val)全部填充
void fill(int[] a, int fromIndex, int toIndex, int val)填充指定索引的元素 左閉右開
五、Arrays.sort對數組排序(使用Arrays.調用)
void sort(int[] a)全部排序 默認升序
void sort(int[] a, int fromIndex, int toIndex)排序指定索引的元素
六、檢查數組中是否包含某一個值
使用Arrays.asList()將Array轉換成List<String>,這樣就可以用動態鏈表的contains函數來判斷元素是否包含在鏈表中
String[] stringArray = { "a", "b", "c", "d", "e" };
boolean b = Arrays.asList(stringArray).contains("a");
System.out.println(b);
七、數組翻轉
Collections.reverse(ArrayList) 將數組進行反轉