Question: Java programming question: Here is the feedback I received misses the requirements. The actual question: Using the array initializer syntax , create an
Java programming question:
Here is the feedback I received "misses the requirements."
The actual question:
Using the array initializer syntax, create an int array holding 8 to 10 small numbers. Then create an ArrayList to hold Integer objects. Use a foreach loop to assign all elements of the array to the arrayList. Process the ArrayList with a for loop to display the elements all on one line separated by spaces. Use the Collections class to report the largest and smallest elements in the ArrayList. Sortthe ArrayList and use another foreach loop to display the sorted elements all on one line separated by spaces.
Sample Output
12 23 9 17 11 16 10 6
The largest element was 23
The smallest element was 6
6 9 10 11 12 16 17 23
Lowest element is 6
________________________________________________________________________________________
My code:
import java.util.*;
public class Program4 {
public static void main(String[] args) { int[] list = new int[10];
int count = 0; int high = 0;
for (int num : list) {
list[count] = (int) (Math.random() * +(100)); count++; }
int low = list[0]; count = 0; for (int jcount : list) {
if (list[count] < low) { low = list[count];
}
if (list[count] > high) { high = list[count];
} System.out.print(list[count] + " "); count++; }
System.out.println(" Highest element " + high); System.out.println(" Lowest element " + low); Arrays.sort(list); System.out.println("sorted " + Arrays.toString(list));
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
