What is the Simplest Way to Reverse an ArrayList?

前端 未结 10 974
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 18:10

What is the simplest way to reverse this ArrayList?

ArrayList aList = new ArrayList<>();

//Add elements to ArrayList object
aList.add(\         


        
10条回答
  •  执笔经年
    2020-11-28 18:27

    Collections.reverse(aList);
    

    Example (Reference):

    ArrayList aList = new ArrayList();
    //Add elements to ArrayList object
    aList.add("1");
    aList.add("2");
    aList.add("3");
    aList.add("4");
    aList.add("5");
    Collections.reverse(aList);
    System.out.println("After Reverse Order, ArrayList Contains : " + aList);
    

提交回复
热议问题