一.如何定义数组:
1st. int[] iset = new int[3];
2nd. int[] iset = new int[]{17,21,19};
3rd. int[] iset = {1,2,3};
二如何遍历数组:
1st.for + [i]
for(int i = 0;i<iset.length;i++){
System.out.println(iset[i]);
}
2nd.foreach forin JDK5.0新特性 Tiger's
for(int i : iset){
System.out.println(i);
}
三.java中的排序
1st. int[] iset = new int[3];
2nd. int[] iset = new int[]{17,21,19};
3rd. int[] iset = {1,2,3};
二如何遍历数组:
1st.for + [i]
for(int i = 0;i<iset.length;i++){
System.out.println(iset[i]);
}
2nd.foreach forin JDK5.0新特性 Tiger's
for(int i : iset){
System.out.println(i);
}
三.java中的排序
//java中自带的数组的升序排序 是由java.util.Arrays提供
public static void sortArray(){
int[] arrayInfo ={10,78,65,23,99,100};
Arrays.sort(arrayInfo);
for(int i :arrayInfo){
System.out.println(i);
}
}
java中降序的排序
//java中降序排序 使用异或运算符
public static void descending(){
int [] arrayInfo = new int[]{10,78,65,23,99,100};
for(int i =0;i<arrayInfo.length-1;i++){
for(int j = 0;j<arrayInfo.length-1-i;j++){
if(arrayInfo[j] < arrayInfo[j+1]){
arrayInfo[j]=arrayInfo[j]^arrayInfo[j+1];
arrayInfo[j+1]=arrayInfo[j]^arrayInfo[j+1];
arrayInfo[j]=arrayInfo[j]^arrayInfo[j+1];
}
}
}
for(int s :arrayInfo){
System.out.println(s);
}
}
数组复制
//数组复制
public static void arrayCopy(){
int[] iset1 = new int[]{99,68,59,33};
int[] iset2 = new int[iset1.length << 1];//给iset2多扩容用来纳入其他数据
System.arraycopy(iset1, 0, iset2, 0, iset1.length);
for(int i :iset2){
System.out.println(i);
}
}