写一个函数reverseList,该函数能够接受一个List,然后把该List 倒序排列。 例如: List list = new ArrayList(); list.add(“Hello”); list.add(“World”); list.add(“Learn”); //此时list 为Hello World Learn reverseList
package homework004; import java.util.ArrayList; import java.util.List; public class Daoxu { public void reverseList(List<String> list) { List<String> list1 = new ArrayList<>(); for(int i =list.size()-1;i>=0;i--) { list1.add(list.get(i)); } list= list1; System.out.println("list="+list); } public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Fly"); list.add("Can"); list.add("You"); list.add("Believe"); list.add("I"); System.out.println("list="+list); Daoxu dx = new Daoxu(); System.out.println("倒序排列后:"); dx.reverseList(list); } }