Answers for "reverse method java arraylist"

5

how to reverse order of arraylist

ArrayList<Integer> arrlist = new ArrayList<Integer>();
//This is how to reverse arrlist's oringinal order.
Collections.reverse(arrlist);

//This is how to sort in a reverse order
 Collections.sort(arrlist, Collections.reverseOrder());
Posted by: Guest on September-06-2020
0

Arraylist reverse in ArrayList in java

I want to store them in another ArrayList
        ArrayList<Integer>list1 = new ArrayList<>();
        for(int a = 1; a < 8; a++){
            list1.add(a);
}
   System.out.println(list1); // [1, 2, 3, 4, 5, 6, 7]

ArrayList<Integer>reverse = new ArrayList<>(); //reversleri bunun icine ekledik
  for (int a = list1.size()-1; a >= 0; a--) {
 // int each = list1.get(a);  // unboxing altaki ile ayni
      reverse.add(list1.get(a));
}
System.out.println(reverse); // [7, 6, 5, 4, 3, 2, 1]
}
Posted by: Guest on June-04-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language