Code debug import java.util.ArrayList; public class ArrayList { public static ArrayList reverse(ArrayList list) { for (int i = 0; i < list.size(); i++) { System.out.println(list); } public static ArrayList getReverse(ArrayList list){ for (int index = 0; index < list.size() / 2; index++) { int temp = list.get(index); list.set(index, list.get(list.size() - index - 1));//swap list.set(list.size() - index - 1, temp); //swap }return list; } } public static void main(String[] args) { ArrayList list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); System.out.println("Original elements : " + list); System.out.println("Reversed elements : " + getReverse(list)); } }
Code debug
import java.util.ArrayList;
public class ArrayList {
public static ArrayList<Integer> reverse(ArrayList<Integer> list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list);
}
public static ArrayList<Integer> getReverse(ArrayList<Integer> list){
for (int index = 0; index < list.size() / 2; index++) {
int temp = list.get(index);
list.set(index, list.get(list.size() - index - 1));//swap
list.set(list.size() - index - 1, temp); //swap
}return list;
}
}
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
System.out.println("Original elements : " + list);
System.out.println("Reversed elements : " + getReverse(list));
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images