数组反转
解题思路
-
定义2个数组arrays和result,将result[4]=arrays[0],result[3]=arrays[1],以此类推。
-
输出result[5]
package com.yicurtain.array;
public class demo02 {
public static void main(String[] args) {
int[] arrays=new int[5];
arrays[0]=1;
arrays[1]=2;
arrays[2]=3;
arrays[3]=4;
arrays[4]=5;
int[] reverse = reverse(arrays);
for (int num:reverse
) {
System.out.println(num);
}
/*
for (int j=0;j<reverse.length;j++) {
System.out.println(reverse[j]);
}*/
}
public static int[] reverse(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;
}
}