int[]s={1,2,3,4,5,6};怎么反序 {6,5,4,3,2,1}
6 回答
已采纳
XhstormR
TA贡献140条经验 获得超197个赞
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Test { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); int[] s = {1, 2, 3, 4, 5, 6}; for (int i : s) { list.add(i); } Collections.reverse(list); Object[] objects = list.toArray(); System.out.println(Arrays.toString(objects)); } }
代码以上,望采纳。
阿旭_
TA贡献217条经验 获得超353个赞
import java.util.Arrays; public class Test8 { public static void main(String[] args) { // int[]s={1,2,3,4,5,6};怎么反序 {6,5,4,3,2,1} int[] s = { 1, 2, 3, 4, 5, 6 }; for (int i = 0; i < s.length / 2; i++) { s[i] = s[i] ^ s[s.length - 1 - i]; s[s.length - 1 - i] = s[i] ^ s[s.length - 1 - i]; s[i] = s[i] ^ s[s.length - 1 - i]; } System.out.println("反序后:" + Arrays.toString(s)); } }
AatroxC
TA贡献6条经验 获得超1个赞
import java.util.Arrays; public class Test8 { public static void main(String[] args) { // int[]s={1,2,3,4,5,6};怎么反序 {6,5,4,3,2,1} int[] s = { 1, 2, 3, 4, 5, 6 }; for (int i = 0; i < s.length / 2; i++) { s[i] = s[i] ^ s[s.length - 1 - i]; s[s.length - 1 - i] = s[i] ^ s[s.length - 1 - i]; s[i] = s[i] ^ s[s.length - 1 - i]; } System.out.println("反序后:" + Arrays.toString(s)); } }
慕粉鸣少
TA贡献1条经验 获得超1个赞
public class main{
public static void main(String[] args){
int[] a={1,2,3,4,5,6};
for(int i=a.length-1;i>=0;i--){
system.out.print(a[i]);
}
}
}//一条代码即可,请采纳
qq_抱嫦娥O烤玉兔_03445798
TA贡献5条经验 获得超1个赞
int[] a = { 1, 2, 3, 4, 5, 6 }; for (int i = a.length - 1; i >= 0; i--) { System.out.print(a[i]); }
田心枫
TA贡献1064条经验 获得超383个赞
import java.util.ArrayList;import java.util.Collections; public class Main { public static void main(String[] args) { ArrayList arrayList = new ArrayList(); arrayList.add("A"); arrayList.add("B"); arrayList.add("C"); arrayList.add("D"); arrayList.add("E"); System.out.println("反转前排序: " + arrayList); Collections.reverse(arrayList); System.out.println("反转后排序: " + arrayList); } }
反转前排序: [A, B, C, D, E] 反转后排序: [E, D, C, B, A]
添加回答
举报
0/150
提交
取消