Transcript ppt

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
Arrays: what are the positions?
• every item has a place; a position number.
place
1.
2.
3.
4.
5.
6.
7.
8.
item
milk
bread
eggs
frozen pizza
juice
apples
oranges
Ramen noodles
Arrays: how are they numbered?
• Java begins numbering at 0.
place
0.
1.
2.
3.
4.
5.
6.
7.
item
milk
bread
eggs
frozen pizza
juice
apples
oranges
Ramen noodles
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]
groceryList [1]
groceryList [2]
groceryList [3]
groceryList [4]
groceryList [5]
groceryList [6]
groceryList [7]
=
=
=
=
=
=
=
=
milk
bread
eggs
frozen pizza
juice
apples
oranges
Ramen noodles
Arrays: what are the elements?
• The individual items are called elements.
The reference numbers are position numbers,or
subscripts.
groceryList [0]
groceryList [1]
groceryList [2]
groceryList [3]
groceryList [4]
groceryList [5]
groceryList [6]
groceryList [7]
=
=
=
=
=
=
=
=
milk
elements
bread
eggs
frozen pizza
juice
subscripts
apples
oranges
Ramen noodles
Arrays: about the position numbers
Subscripts must be integers or integer expressions.
groceryList [0]
groceryList [1]
groceryList [2]
groceryList [3]
groceryList [4]
groceryList [5]
groceryList [6]
groceryList [7]
=
=
=
=
=
=
=
=
milk
bread
eggs
frozen pizza
juice
apples
oranges
Ramen noodles
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
Declaring and allocating an array
• to declare and allocate an array:
int list[];
list = new int[7];
or
int list[] = new int[7];
list [ 0 ] = 5
list [ 1 ] = 10
list [ 2 ] = -3
list [ 3 ] = -7
list [ 4 ] = 8
list [ 5 ] = 1
list [ 6 ] = 9
Another example:
String args[] = new String[5];
Declaring & initializing an array
• to declare and initialize an array:
• int list[] = {5,10,-3,-7,8,1,9};
list [ 0 ] = 5
list [ 1 ] = 10
list [ 2 ] = -3
list [ 3 ] = -7
list [ 4 ] = 8
list [ 5 ] = 1
list [ 6 ] = 9
initializing elements of an array
• to initialize an array element:
• list[6] = 9;
list [ 0 ] = 5
list [ 1 ] = 10
list [ 2 ] = -3
list [ 3 ] = -7
list [ 4 ] = 8
list [ 5 ] = 1
list [ 6 ] = 9
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,
1, 6, 3, 8, 6,10,
6, 5, 7, 6, 8, 6,
5, 6, 7, 5, 6, 4,
int frequency[] = new int [ 11 ];
String output = "";
)
9,
3,
7,
8,
7,
8,
5,
6,
8,10,
2, 7,
6, 6,
8,10};
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";
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 );
}
}
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.
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 )
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
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
Sorting and Searching Arrays
•
•
•
•
Look at PassArray.java
Sorting arrays: the bubble sort method
Searching arrays: the linear search
Searching arrays: the binary search
PassArray.java -- pt. 1
//Fig 7.10: PassArray.java
//Passing arrays and individual elements to methods
import 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 );
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";
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 );
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;
}
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.
3619
3619
3169
3169
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.
3619
3619
3169
3169
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.
3619
3619
3169
3169
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.
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 ;
}
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!
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.
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.