(List)写一个函数reverseList,
该函数能够接受一个List,
然后把该List 倒序排列。
例如:
List list = new ArrayList();
list.add(“Hello”); list.add(“World”); list.add(“Learn”);
//此时list 为Hello World Learn
reverseList(list);
//调用reverseList 方法之后,list 为Learn World Hello
1 import java.util.ArrayList; 2 import java.util.List; 3 4 public class TestDay17_3 { 5 6 public static void main(String[] args) { 7 List<String> list = new ArrayList<String>(); 8 list.add("Hello"); 9 list.add("World"); 10 list.add("Learn"); // 此时list 为Hello World Learn 11 String s = reverseList(list); 12 System.out.println("翻转后:" + s); 13 } 14 15 public static String reverseList(List<String> l) { 16 //遍历一半的集合,i从前往后,j从后王前 17 for (int i = 0, j = l.size()-1; i < l.size() / 2; i++, j--) { 18 //元素调换 19 String s = l.get(i); 20 l.set(i, l.get(j)); 21 l.set(j, s); 22 } 23 return l.toString(); 24 25 } 26 }