int[] arr = new int[]{34,54,3,2,65,7,34,5,76,34,67}; for(int i = 0;i < arr.length/2;i++) { int temp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = temp; } for(int i = 0;i < arr.length;i++) { System.out.print(arr[i] + "\t"); } System.out.println(); //互换方法2 int[] newArr = new int[arr.length]; for(int i = arr.length - 1;i >= 0;i--) { newArr[arr.length - 1 - i] = arr[i]; System.out.print(arr[i]+"\t"); } System.out.println(); //互换方法3 for(int i = 0,j = arr.length - 1;i < j;i++,j--) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; System.out.print(arr[i]+"\t"); }
int[] arr = new int[]{34,54,3,2,65,7,34,5,76,34,67};
for(int i = 0;i < arr.length/2;i++)
{
int temp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}文章来源:https://www.toymoban.com/news/detail-415834.htmlfor(int i = 0;i < arr.length;i++)
{
System.out.print(arr[i] + "\t");
}
System.out.println();
//互换方法2
int[] newArr = new int[arr.length];
for(int i = arr.length - 1;i >= 0;i--)
{
newArr[arr.length - 1 - i] = arr[i];
System.out.print(arr[i]+"\t");
}
System.out.println();
//互换方法3
for(int i = 0,j = arr.length - 1;i < j;i++,j--)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
System.out.print(arr[i]+"\t");
}文章来源地址https://www.toymoban.com/news/detail-415834.html
到了这里,关于JAVA数组元素反转的三种写法------JAVA入门基础教程的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!