六、Java数组及操作

1、数组初始化

1.1 一维数组

数组元素类型 数组名 = new 数组元素类型[数组元素的个数]

int month = new int[12];

1.2 二维数组

int myarr[][] = new int[2][4];

int myarr[2][2] = {{2,2},{1,2}};

2、数组基本操作

  1. 遍历数组
  2. 填充替换数组元素
    • fill(int[] a,int value)
      • a:为进行替换的数组
      • value:要存储数组中所有元素的值
    • fill(int[] a,int fromIndex,int toIndex,int value)
      • a:为进行替换的数组
      • value:要存储数组中所有元素的值
      • fromIndex:开始索引(包含)
      • toIndex:结束索引(不包含)
  3. 对数组进行排序Arrays.sort()
  4. 复制数组:使用Arrays类的copyOf
    1. copyOf(arr,int newlength)
    • arr:要进行复制的数组
    • newlength:复制后的新数组的长度。超出复制数组补零,小于则截取到所需长度位置。
      2.copyOfRange(arr,int fromIndex,int toIndex)

3、数组的排序方式

谁不是孤身一人,翻山越岭
原文地址:https://www.cnblogs.com/hasz/p/12250696.html