31
11/15: Ch. 7: Arrays • What is an array? • Declaring & allocating arrays • Sorting & searching arrays

11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

Embed Size (px)

Citation preview

Page 1: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

11/15: Ch. 7: Arrays

• What is an array?

• Declaring & allocating arrays

• Sorting & searching arrays

Page 2: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

Arrays: what are they?

• a series of elements; a list.

• grocery list could be an array: – milk– bread– eggs– frozen pizza– juice– apples– oranges– Ramen noodles

Page 3: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

Arrays: what are the positions?

• every item has a place; a position number. place item

1. milk

2. bread

3. eggs

4. frozen pizza

5. juice

6. apples

7. oranges

8. Ramen noodles

Page 4: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

Arrays: how are they numbered?

• Java begins numbering at 0. place item

0. milk

1. bread

2. eggs

3. frozen pizza

4. juice

5. apples

6. oranges

7. Ramen noodles

Page 5: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

Arrays: how are they named?

• Java names the list and refers to the place of the items in square brackets after the list name. groceryList [0] = milk

groceryList [1] = bread

groceryList [2] = eggs

groceryList [3] = frozen pizza

groceryList [4] = juice

groceryList [5] = apples

groceryList [6] = oranges

groceryList [7] = Ramen noodles

Page 6: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

Arrays: what are the elements?• The individual items are called elements.

The reference numbers are position numbers,or subscripts.groceryList [0] = milkgroceryList [1] = breadgroceryList [2] = eggsgroceryList [3] = frozen pizzagroceryList [4] = juicegroceryList [5] = applesgroceryList [6] = orangesgroceryList [7] = Ramen noodles

elements

subscripts

Page 7: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

Arrays: about the position numbers

Subscripts must be integers or integer expressions. groceryList [0] = milk

groceryList [1] = bread

groceryList [2] = eggs

groceryList [3] = frozen pizza

groceryList [4] = juice

groceryList [5] = apples

groceryList [6] = oranges

groceryList [7] = Ramen noodles

Page 8: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

Arrays: function examples in Java

• an array of integers:list [ 0 ] = 5

list [ 1 ] = 10

list [ 2 ] = -3

list [ 3 ] = -7

list [ 4 ] = 8

list [ 5 ] = 1

list [ 6 ] = 9

list.length = 7

list [0] + list [2] = 2

list [1 + 3] = 8

Page 9: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

Declaring and allocating an array

• to declare and allocate an array:

int list[];

list = new int[7];

or

int list[] = new int[7];

Another example:

String args[] = new String[5];

list [ 0 ] = 5list [ 1 ] = 10list [ 2 ] = -3list [ 3 ] = -7list [ 4 ] = 8list [ 5 ] = 1list [ 6 ] = 9

Page 10: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

Declaring & initializing an array

• to declare and initialize an array:

• int list[] = {5,10,-3,-7,8,1,9};

list [ 0 ] = 5list [ 1 ] = 10list [ 2 ] = -3list [ 3 ] = -7list [ 4 ] = 8list [ 5 ] = 1list [ 6 ] = 9

Page 11: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

initializing elements of an array

• to initialize an array element:

• list[6] = 9;

list [ 0 ] = 5list [ 1 ] = 10list [ 2 ] = -3list [ 3 ] = -7list [ 4 ] = 8list [ 5 ] = 1list [ 6 ] = 9

Page 12: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

StudentPoll.java: pt. 1//fig. 7.7: StudentPoll.java

import javax.swing.*;

public class StudentPoll { public static void main ( String args[] ) { int responses[] = { 1, 2, 6, 4, 8, 5, 9, 7, 8,10, 1, 6, 3, 8, 6,10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8,10}; int frequency[] = new int [ 11 ]; String output = "";

Page 13: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

About StudentPoll.java: pt. 2 for ( int a = 0 ; a < responses.length; a++ ) ++frequency [ responses [ a ] ];

output += "Rating\tFrequency\n";

for ( int r = 1; r < frequency.length ; r++ ) output += r + "\t" + frequency[r] + "\n";

Page 14: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

About StudentPoll.java: pt. 3 JTextArea outputArea = new JTextArea (11,10); outputArea.setText( output );

JOptionPane.showMessageDialog ( null , outputArea ,

"Student Poll Program" , JOptionPane.INFORMATION_MESSAGE );

System.exit ( 0 ); }}

Page 15: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

About “.length”

• length is actually an instance variable, indicating the number of elements in the array.

• We don’t have to declare it separately – it’s an automatic part of an array that we declare.

Page 16: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

Passing arrays to methods

• Two ways: – passing the entire array as a whole– passing individual elements of the array

• The method that modifies the array must have an appropriate input type (parameter).

• Accepting an array: public void x ( int a[] )

• Accepting elements: public void y ( int c )public void z ( String d )

Page 17: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

Getting more experience with arrays

• Pg 273: InitArray.java – declaring & allocating

• Pg 274: InitArray.java – initializing to values

• Pg 275: InitArray.java – using a for loop to set values

• Pg 276: SumArray.java – using a for loop to add up elements of an array

Page 18: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

1st Programs of the day

• pg. 278: StudentPoll.java

• After getting the program to run, modify the program to allow the user to input the array elements.– use a JOptionPane.showInputDialog to get the data

from the user, and assign the CONVERTED value you get from there into the next array position. Use a for loop to provide the repetition and movement in the array position.

• Pg. 284 PassArray.java

Page 19: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

Sorting and Searching Arrays

• Look at PassArray.java

• Sorting arrays: the bubble sort method

• Searching arrays: the linear search

• Searching arrays: the binary search

Page 20: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

PassArray.java -- pt. 1//Fig 7.10: PassArray.java//Passing arrays and individual elements to methodsimport java.awt.Container;import javax.swing.*;

public class PassArray extends JApplet { JTextArea outputArea; String output; public void init() { outputArea = new JTextArea(); Container c = getContentPane(); c.add( outputArea );

Page 21: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

PassArray.java -- pt. 2 int a[] = { 1, 2, 3, 4, 5 }; output = "Effects of passing entire " + "array call-by-reference:\n" + "The values of the original array are:\n"; for ( int i = 0; i < a.length ; i++ ) output += " " + a[ i ]; modifyArray ( a ); //passing the whole array output+="\n\nValues of the modified array are:\

n";

Page 22: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

PassArray.java -- pt. 3 for ( int i = 0; i < a.length ; i++ ) output += " " + a[ i ]; output += "\n\nEffects of passing array " + "element call-by-value:\n" + "a[3] before modifyElement: " + a[ 3 ]; modifyElement ( a [ 3 ] ); output += "\na[3] after modifyElement: " + a

[ 3 ]; outputArea.setText ( output ); }

Page 23: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

PassArray.java -- pt. 4 public void modifyArray ( int b[] ) { for ( int j = 0 ; j < b.length; j++ ) b [ j ] *= 2; } public void modifyElement ( int e ) { e *= 2; }}

Page 24: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

Sorting Arrays using Bubble Sort

• Reorganizing an array in some order (low to high, etc.)

• Bubble Sort compares two values, switches them in the array positions if appropriate, and checks the next two values.

3 6 1 9

3 6 1 9

3 1 6 9

3 1 6 9

Page 25: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

Sorting Arrays using Bubble Sort

• In this case, 3 & 6 are compared, and NOT switched.

• Then, 6 & 1 are compared, then switched.

• Then 6 & 9 are compared and NOT switched.

• This is ONLY ONE PASS through the array.

3 6 1 9

3 6 1 9

3 1 6 9

3 1 6 9

Page 26: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

Sorting Arrays using Bubble Sort

• Core of sorting: an if structure.

• This is nested inside a for loop to look at each pair of values in the array.

• This loop in nested inside another loop to make multiple passes through the array.

• Look at SortThem & its source code.

3 6 1 9

3 6 1 9

3 1 6 9

3 1 6 9

Page 27: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

Searching Arrays: Linear Search

• How do you find a particular element value in an array?

• One way: a linear search.

• The core structure: an if statement in a for loop.

• The for loop gives movement through the array

• The if structure looks for a matching value in the elements.

Page 28: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

Searching Arrays: Linear Search

• The for loop gives movement through the array

• The if structure looks for a matching value in the elements.

for ( int n = 0 ; n < array.length ; n++ ) {if ( array [ n ] == key )

return n ;}

Page 29: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

Searching Arrays: Binary Sort

• Go to the middle of the array.

• See if the number you are looking for is higher or lower, and go to the middle of that side.

• Repeat until found.

• Note that the array must already be sorted!

Page 30: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

Searching Arrays: Linear vs. Binary

• A linear search works well for smaller arrays, and is simpler to understand and troubleshoot.

• A binary search is better for a large array. It is more efficient in its searching.

Page 31: 11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays

Program of the Day: BinarySearch

• pg. 291 BinarySearch.java

• Once you get it to work, figure out how it works.

• Next time: review searching and learn about multiple-subscripted arrays.