public class ArrayDemo{
public static void main(String args[]){
int data[]=new int[]{1,2,3,4,5,6,7,8} ;
int temp[]=new int[data.length];
print(data);
int foot=data.length-1;
for(int x=0;x<temp.length;x++)
{
temp[x]=data[foot];
foot--;
}
print(temp);
}
public static void print(int temp[]){
for(int x=0;x<temp.length;x++){
System.out.print(temp[x]+",");
}
System.out.println();
}
}
以上的代码可以实现代码转置不过会产生垃圾。
public class ArrayDemo{
public static void main(String args[]){
int data[]=new int[]{1,2,3,4,5,6,7,8} ;
print(data);
reverse(data);
print(data);
}
public static void reverse(int temp[]){
int foot=temp.length/2;
int head=0;
int tail=temp.length-1;
for(int i=0;i<=foot;i++){
int t=temp[head];
temp[head]=temp[tail];
temp[tail]=t;
head++;
tail--;
}
}
public static void print(int temp[]){
for(int x=0;x<temp.length;x++){
System.out.print(temp[x]+",");
}
System.out.println();
}
}