MrDoyle

Arrays) Locating the Largest Value in an Array

Jan 25th, 2021
477
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Random;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         Integer[] Array = new Integer[10];
  7.         Random rand = new Random();
  8.  
  9.         System.out.println();
  10.         for (int i = 0; i < Array.length; i++) {
  11.             int rand_int1 = rand.nextInt(101);
  12.             Array[i] = rand_int1;
  13.         }
  14.  
  15.         System.out.print("Array: ");
  16.         for (int i = 0; i < Array.length; i++) {
  17.             System.out.print(Array[i] + " ");
  18.         }
  19.  
  20.         int large = 0;
  21.         int location = 0;
  22.  
  23.         for (int i = 0; i < Array.length; i++) {
  24.             if (Array[i] > large) {
  25.                 large = Array[i];
  26.                 location = i;
  27.             }
  28.         }
  29.  
  30.         System.out.println("\n\nThe largest value is " + large);
  31.         System.out.println("It is in slot " + location);
  32.     }
  33. }
RAW Paste Data