Arrays工具类的使用
package com.array;
import java.util.Arrays;
public class TestArrays {
public static void main(String[] args) {
int[] a={2,5,6,9,7};
System.out.println(a);
System.out.println(Arrays.toString(a));
Arrays.sort(a);
System.out.println(Arrays.toString(a));
System.out.println(Arrays.binarySearch(a, 6));//如果存在6,则放回索引,不存在放回-1
}
}
多维数组
package com.array;
public class Test2DimensionArray {
public static void main(String[] args) {
int[][] a=new int[3][];
a[0]=new int[]{2,3};
a[1]=new int[]{55,66,77};
a[2]=new int[]{8,9};
System.out.println(a[1][2]);//77
//初始化静态数组
int[][] b={
{6,6,9},
{8,5,9},
{2,4},
};
System.out.println(b[1][0]);//8
}
}
多维数组存储表格数据
package com.array;
import java.util.Arrays;
public class TestArrayTableData {
public static void main(String[] args) {
Object[] emp1={100,"张",20};
Object[] emp2={101,"李",19};
Object[] emp3={102,"黄",21};
Object[][] tableData=new Object[3][];
tableData[0]=emp1;
tableData[1]=emp2;
tableData[2]=emp3;
for(Object[] temp:tableData){
System.out.println(Arrays.toString(temp));
}
}
}
输出
[100, 张, 20]
[101, 李, 19]
[102, 黄, 21]