ArrayListTestRandom

ArrayListTestRandom - int large = rNums.get(0); // Use for...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
//Generate a collection of 20 random integers //and find the largest in the collection / import java.util.ArrayList; import java.util.Iterator; import java.util.Random; i public class ArrayListTestRandom { public static void main (String [] arg) { final int MAXELTS = 20; Random generator = new Random(); ArrayList<Integer> rNums = new ArrayList<Integer>(); for(int k = 0; k < MAXELTS; k++) { rNums.add(generator.nextInt(100) + 1); } System.out.println(rNums); //Find largest value //Let 0th value be the largest
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: int large = rNums.get(0); // Use for loop to iterate for (int i = 0; i < rNums.size(); i++) { int tempLarge = rNums.get(i); if(tempLarge > large) large = tempLarge; } System.out.println("The largest value is: " + large); //Use for-each loop to iterate until we find a larger value for (int r : rNums) { int tempLarge = r; if(tempLarge > large ) large = tempLarge; } System.out.println("The largest value is: " + large); } }...
View Full Document

This note was uploaded on 06/07/2010 for the course CS 201 taught by Professor Staff during the Spring '08 term at Iona.

Ask a homework question - tutors are online