






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Department of Computer Science Wellesley College
Linear collections 18-
single objects (Buggles, ints, and things …). Buggle becky result 5
with large collections of things.
big class. Buggle becky result 5
until lists came along and the cloud went away for a while.
Linear collections 18- Arrays
*No casting. Linear collections 18- Array notation 0.0 0.0 0.0 0.0 0. 0 1 2 3 4 average
type of array elements number of elements name of array variable
Linear collections 18- The buggleBunch
buggleBunch 0 1 2 Buggle Buggle NORTH (1,1) blue true EAST (2,6) red true null Buggle Linear collections 18- Swapping array elements public static void swap (int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; }
Linear collections 18- Sum up the elements of an int array 15 23 19 25 12 0 1 2 3 4 a public static ___ sumIntArray(___ __ a) { int sum = 0 ; return sum; } Linear collections 18- Iterating over an array public static int sumIntArray(int [] a) { int sum = 0 ; for (int i= 0 ; i<a.length; i++) { sum = sum + a[i]; } return sum; } 15 23 19 25 12 0 1 2 3 4 a
Linear collections 18- minBetween() 15 23 19 25 12 0 1 2 3 4 a public static int minBetween(int [] a, int lo, int hi) { int minVal = Integer.MAX_VALUE; // return min integer in array between lo and hi return minVal; } minBetween(a, 1, 3) only looks for minVal here Linear collections 18- minBetween() 15 23 19 25 12 0 1 2 3 4 a public static int minBetween(int [] a, int lo, int hi) { int minVal = Integer.MAX_VALUE; for (int i = lo; i <= hi; i++) { if (a[i] < minVal) { minVal = a[i]; } } return minVal; } change limits of search so that minBetween(a, 1, 3) looks between lo & hi
Linear collections 18- public static int minIndexBetween(int [] a,int lo,int hi) { int minVal = Integer.MAX_VALUE; for (int i = lo; i <= hi; i++) { if (a[i] < minVal) { minVal = a[i]; } } return minIndex; } What changes do we have to make … 15 23 19 25 12 a 0 1 2 3 4 return the index of minVal between lo & hi Linear collections 18- public static int minIndex(int [] a, int lo, int hi) { int minVal = Integer.MAX_VALUE; int minIndex = -1; for (int i = lo; i <= hi; i++) { if (a[i] < minVal) { minVal = a[i]; minIndex = i; } } return minIndex; } minIndexBetween() 15 23 19 25 12 a 0 1 2 3 4 return the index of minVal between lo & hi
Linear collections 18- Good news & bad news
15 23 19 20 12 0 1 2 3 4 a “A” “B” “C” (^) Ø p