What is the Simplest Way to Reverse an ArrayList?

前端 未结 10 1022
伪装坚强ぢ
伪装坚强ぢ 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:18

    ArrayList myArray = new ArrayList();
    
    myArray.add(1);
    myArray.add(2);
    myArray.add(3);
    
    int reverseArrayCounter = myArray.size() - 1;
    
    for (int i = reverseArrayCounter; i >= 0; i--) {
        System.out.println(myArray.get(i));
    }
    

提交回复
热议问题