数组是引用数据类型,有内存的分配问题。需要声明并初始化数组。
eg:开辟一个长度为3的数组
int x = new int[3];
分立实现数组实例化
int [ ] x = null;
x = new int[3];
二维数组
int [ ] [ ] data = new int [ ] [ ]{{1,2,3},{4,5},{6,7,8}};
java实现数组排序
java.util.Arrays.sort(arrayname);
public static void main(String[] args) {
int[] intData = new int[]{7, 6, 3, 4, 5};
char[] charData = new char[]{'z', 'g', 'c', 'd'};
java.util.Arrays.sort(intData);
java.util.Arrays.sort(charData);
printArray(intData);
printArray(charData);
}
public static void printArray(int [] temp){
for (int i = 0;i<temp.length;i++){
System.out.println(temp[i]);
}
System.out.println();
}
public static void printArray (char[]temp){
for (int i = 0;i<temp.length;i++){
System.out.println(temp[i]);
}
System.out.println();
}
Java实现数组拷贝
public class Test {
public static void main(String[] args) {
int [] dataA = new int []{1,2,3,4,5,6,7,8};
int [] dataB = new int []{11,22,33,44,55,66,77,88};
System.arraycopy(dataB,4,dataA,2,4);
printArray(dataA);
}
public static void printArray(int [] temp) {
for(int i=0;i<temp.length;i++){
System.out.println(temp[i]);
}
System.out.println();
}
}