Docsity

Linear Collections: Understanding Arrays in Java, Study notes of Computer Science

Toggle sidebar
bg1
1
CS111 Computer Programming
Department of Computer Science
Wellesley College
Linear collections
Arrays
Linear collections 18-2
A bedtime story
Once upon a time our variables referred to
single objects (Buggles, ints, and things …).
Buggle
becky
result
5
But we were sad because we wanted to work
with large collections of things.
We tried bundling all our things into one
big class.
Buggle
becky
result
5
This brought a dark cloud over our heads,
until lists came along and the cloud went away
for a while.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Linear Collections: Understanding Arrays in Java and more Study notes Computer Science in PDF only on Docsity!

CS111 Computer Programming

Department of Computer Science Wellesley College

Linear collections

Arrays

Linear collections 18-

A bedtime story

Once upon a time our variables referred to

single objects (Buggles, ints, and things …). Buggle becky result 5

But we were sad because we wanted to work

with large collections of things.

We tried bundling all our things into one

big class. Buggle becky result 5

This brought a dark cloud over our heads,

until lists came along and the cloud went away for a while.

Linear collections 18- Arrays

o An array is an ordered

collection of elements all

of the same type or

class.*

o In Java arrays are

objects of a fixed length.

o Arrays are very good if

you want to get to

something quickly.

*No casting. Linear collections 18- Array notation 0.0 0.0 0.0 0.0 0. 0 1 2 3 4 average

float [] averages = new float[ 5 ];

type of array elements number of elements name of array variable

Linear collections 18- The buggleBunch

Buggle [] buggleBunch = new Buggle[ 3 ];

buggleBunch[ 0 ] = new Buggle();

buggleBunch[ 0 ].setColor(Color.blue);

buggleBunch[ 0 ].setHeading(Direction.NORTH);

buggleBunch[ 1 ] = new Buggle();

buggleBunch[ 1 ].setPosition(new Point( 2 , 6 ));

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

Lists Arrays

Find first element

Find last element

Add element to front

Add element to end

Find length

Mutable

Work for diff types

15 23 19 20 12 0 1 2 3 4 a “A” “B” “C” (^) Ø p