Question & Answer: Create array list with 15 eleemts that has random integer values from 0 to 50. print the arraylist. then create a me…..

Java

Create array list with 15 eleemts that has random integer values from 0 to 50. print the arraylist. then create a method that will sort it in ascending order without using anything from the java library and print the sorted array

Expert Answer

 

import java.util.ArrayList;
import java.util.List;

public class SortList {

public static void main(String[] args) {
List<Integer> a = new ArrayList<Integer>();
a.add(2);
a.add(5);
a.add(4);
a.add(6);
a.add(8);
a.add(3);
a.add(1);
a.add(9);
a.add(45);
a.add(26);

System.out.println(“Elements before sorting: “);
System.out.println(a);
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a.size() – i – 1; j++) {
if (a.get(j) > a.get(j + 1)) {
int temp = a.get(j);
a.set(j, a.get(j + 1));
a.set(j + 1, temp);
}
}
}
System.out.println(“Elements After sorting (in Ascending order): “);
System.out.println(a);
}
}

HERE WE CAN SORT THE ELEMENTS BY USING THE FOR LOOP

Still stressed from student homework?
Get quality assistance from academic writers!