java数组进阶
数组一经定义就不能改变长度
package array;标签:java,进阶,int,arrays,length,numbers,数组,public From: https://www.cnblogs.com/zhazhawei906/p/16782981.html
public class ArrayDemo03 {
public static void main(String[] args) {
int [] numbers={1,2,3,4,5,6,7,8,9,10};
dayin(numbers);
int[] fanzhuan1 = fanzhuan(numbers);//定义了一个新的数组fanzhuan1来存放反转后的数组
dayin(fanzhuan1);
//dayin(fanzhuan(numbers));//也可以用来反转数组,只不过没有定义新的数组
}
//方法
//打印数组
public static void dayin (int[] arrays){
for (int i = 0; i < arrays.length; i++) {
System.out.print(arrays[i]+"\t");
}
}
//反转数组
public static int [] fanzhuan(int[] arrays){
int [] result = new int[arrays.length];
for (int i = 0,j=arrays.length-1; i < arrays.length ; i++,j--) {
result[j]=arrays[i];
}
return result;
}
}