[AppleScript] 纯文本查看 复制代码
package hemowork;
import java.util.ArrayList;
public class Demo3 {
/**
* @param args
* 3,把集合中的元素反转
*
*/
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
list.add("f");
list.add("g");
Object [] arr = list.toArray();//将集合转换为数组
Object temp = "";//定义一个空的变量作为中间变量
for (int i = 0; i < arr.length/2; i++) {
temp = arr;
arr = arr[arr.length-1-i];
arr[arr.length-1-i] = temp;
//System.out.println(arr);
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr+" ");
}
}
}
[Java] 纯文本查看 复制代码
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
list.add("f");
list.add("g");
for (int startIndex = 0, endIndex = list.size() - 1; startIndex < endIndex; startIndex++, endIndex--) {
String start = list.get(startIndex);
String end = list.get(endIndex);
list.set(startIndex, end);
list.set(endIndex, start);
}
System.out.println(list);
}