Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that uses an Integer Array List. The program will prompt th

ID: 3711762 • Letter: W

Question

Write a Java program that uses an Integer Array List. The program will prompt the user for how long the Array List is. The user will then fill up the Array List with integers. Then have the program call a sort method on the Array List to order the list from smallest to largest.

Hint

/* Sorting of arraylist using Collections.sort*/

Collections.sort(arraylist);

The programs output should look similar to the below:

Example)

Standard Output: How long is the list?

Standard Input: 4

Standard Output: Enter the 1th element in the list

Standard Input: 5

Standard Output: Enter the 2th element in the list

Standard Input: 2

Standard Output: Enter the 3th element in the list

Standard Input: 7

Standard Output: Enter the 4th element in the list

Standard Input: 1

Standard Output: Before Sorting: [5, 2, 7, 1]

Standard Output: After Sorting:[1, 2, 5, 7]

Explanation / Answer

Please find the working code below. Comments explaining the working of the line is added with the line itself.

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.Scanner;

public class Chegg {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in); //Scanner for taking input from User

List<Integer> result = new ArrayList<Integer>(); //ArrayList for Storing the input from User

System.out.println("How Long is the List?");

int len = sc.nextInt(); //Getting total length of the list entered by user and storing it into int variable

for(int i=1;i<=len;i++){

System.out.println("Enter "+i+"th element in the list");

result.add(sc.nextInt()); //Getting the list elements from user and directly adding into list by using List.add() method.

}

System.out.println("Before Sorting: "+result.toString());

Collections.sort(result); //Sorting the array by using sort method

System.out.println("After Sorting: "+result.toString());

sc.close();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote