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

Good morning! I\'m trying to write a program that utilizes an Array List of rand

ID: 675399 • Letter: G

Question

Good morning!

I'm trying to write a program that utilizes an Array List of random numbers with the following criteria:

Declares an array list sizes of 10 (an unsorted array list), that is then filled with 10 random numbers from 1-20.

The array list is then copied sorted using java Collections.

Needs to be able to search for a number utilizing a "for-each" loop.

Here are a few screen shots of what I'm looking for:

Sorted Array List 3 5 5 8 Unsorted Array List 19 18 19 5 5 18 19 19 20 Please enter number to search for: 11 Search Ualue: 11 found at location: 1 in the unsorted array Search Ualue 11 found at location 10 in the unsorted array Search Ualue: 11 found at location: 5 in the sorted array Search Ualue: 11 found at location: 6 in the sorted array

Explanation / Answer

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;


public class ArrayRandom {

   public static void main(String []args) {
      
       Scanner scan = new Scanner(System.in);
       ArrayList<Integer> unsortedArr = new ArrayList<Integer>();
      
       //fill array randomly with numbers b/w 1 and 20
       int max = 20;
       int min = 1;
       Random rand = new Random();
       for(int i = 0; i < 10; i++) {
           unsortedArr.add(rand.nextInt((max - min) + 1) + min);
       }
      
       //sort the array
       ArrayList<Integer> sortedArr = new ArrayList<Integer>();
       sortedArr.addAll(unsortedArr);
       Collections.sort(sortedArr);
      
       System.out.println("Unsorted Array List Sorted Array List ");
       for(int i = 0; i < unsortedArr.size(); i++) {
           System.out.println(unsortedArr.get(i)+" "+sortedArr.get(i));
       }
      
       System.out.println("Please enter number to search for:");
       int searchNum = scan.nextInt();
       //search in unsorted list
       int index = 1;
       boolean found1 = false;
       for(Integer n : unsortedArr) {
           if(n == searchNum) {
               found1 = true;
               System.out.println("Search value: "+searchNum+" found at location: "+index+" in the unsorted array");
           }
           index++;
       }
      
      
       //search in unsorted list
       index = 1;
       boolean found2 = false;
       for(Integer n : sortedArr) {
           if(n == searchNum) {
               System.out.println("Search value: "+searchNum+" found at location: "+index+" in the sorted array");
               found2 = true;
           }
           index++;
       }
      
      
       if(!found1 && !found2)
           System.out.println("Search value: "+searchNum+" was not found");
   }
}

--------------------output---------------------

Unsorted Array List                Sorted Array List
19                          1
15                          3
3                          3
19                          4
4                          12
12                          15
17                          16
3                          17
1                          19
16                          19
Please enter number to search for:
3
Search value: 3 found at location: 3 in the unsorted array
Search value: 3 found at location: 8 in the unsorted array
Search value: 3 found at location: 2 in the sorted array
Search value: 3 found at location: 3 in the sorted array

Unsorted Array List                Sorted Array List
2                          2
10                          3
4                          4
11                          5
19                          8
18                          10
16                          11
3                          16
8                          18
5                          19
Please enter number to search for:
20
Search value: 20 was not found

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