ppt - AD Book Enterprises
Download
Report
Transcript ppt - AD Book Enterprises
11/16: Ch. 7: Arrays
• What is an array?
• Declaring & allocating arrays
• Program of the day
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
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