Title: Grouping Data Together
1 Grouping Data Together
- Often we want to group together a number of
values or objects to be treated in the same way
e.g. - names of students in a tutorial group
- exam marks for students in a subject
- items in a catalogue
- orders for gifts
- We need a better technique than creating a
variable for each value or object.
2Arrays
- An array in Java is a collection of values of the
same type . - The elements in an array are held in contiguous
memory locations. - The array class is special. It has a small
number of predefined methods, and a special
notation of its own to invoke them.
3 Logical View of an Array
An array is like a street with houses that can
hold things. The houses are numbered so that
they can be accessed. In some languages
(including Java), the first house number is zero.
In other languages, the numbering starts from 1.
The number of houses is fixed when the array is
created, and cannot be changed without
recompiling the program.
4Declaring Initializing an Array
- One way to create an array with some values in it
is to initialize it when it is created. In this
case, the compiler will make the array as big as
it needs to be to hold the values specified. - int marks 10, 45, 67, 88, 50
10
45
67
88
marks
50
5Examples of Declaring Arrays
- float prices new float500
- int marks 10, 45, 67, 88, 50
- boolean flags
- flags new boolean20
- char codes new char1750
- char letterGrades
- 'A', 'B', 'C', 'D', 'F'
6Array Declarations Revisited
- The brackets of the array type can be associated
with the element type or with the name of the
array - Therefore the following declarations are
equivalent - float prices
- float prices
- The first format is generally more readable
7Command-Line Arguments
- The signature of the main method indicates that
it takes an array of String objects as a
parameter - These values come from command-line arguments
that are provided when the interpreter is invoked - For example, the following invocation of the
interpreter passes an array of three String
objects into main - gt java DoIt pennsylvania texas california
- These strings are stored at indexes 0-2 of the
parameter - See NameTag.java (page 281)
8 Memory Allocation for Array
Array access
Memory map
Address
10
45
67
88
50
9 Accessing an Array
An element is accessed by specifying its position
in the array arrayNameindex where index is a
value (of type int) used to select a particular
element in the array. The lowest valid index is
zero, and the highest is (size of the array) - 1.
10Evaluating an Index
- Any expression that evaluates to an int can be
used as an index (also called a subscript). - Given the following definitions
- int subscript 0
- int marks new int10
- we could access elements by expressions such as
- marks0
- markssubscript 2
- marks10-1
- An index that is outside the array will cause an
ArrayIndexOutOfBoundsException error message.
11 Assigning Values to an Array
You can assign values to particular array
elements. int counter 1 marks4
18 marks0 56 markscounter
38 markscounter 1 87
12Extracting Values from an Array
- A value extracted from an array can be treated
like any other value, e.g. (assuming that the
relevant declarations and initializations have
been done) - System.out.println(marks1)
- total markscounter
- aStudent.calculateTotalMark(markscounter)
13Displaying the Contents of the Array
- public void printArray()
-
- int marks 10, 45, 67, 88, 0, 66
- System.out.println()
- int counter 0
- while (counter lt marks.length)
- System.out.println(markscounter)
- counter
-
14Another Way of Displaying the Array
- public void printArray()
-
- int marks 10, 45, 67, 88, 50
- System.out.println()
- for (int counter 0
- counter lt marks.length counter)
- System.out.println(markscounter)
-
- marks.length returns the number of elements in
the marks array. Note that this is not a method
call. Do not confuse this with the String
class's length() method.
15Creating an Array of floats
- As with any object, the name of an array is a
reference to the array itself. To put aside some
memory, we need to use the new operator. - double prices new double6
16Creating an Array of Objects
- An array of objects is actually an array of
references to objects. - Person family new Person6
These references have not been initialized.
They have NULL values. We have not yet created
the Person objects themselves.
family
17An Array of Objects
- For each element of the array, we need to create
an object that it references.
18Creating the Objects for the Array
- public static void main(String Args)
-
- Person family new Person6
- for (int counter 0 counter lt family.length
- counter)
- familycounter new Person()
- for (int counter 0 counter lt family.length
- counter)
-
- System.out.println("Name?")
- familycounter.setName(Keyboard.readString()
) -
-
19 Passing Arrays as Arguments
- When passing an entire array to a method, the
name of the array is passed without any brackets.
The name of an array is a reference to the
array. - Brackets in the formal parameter show it is an
array, without specifying its size. The array
knows how big it is. - Because a method is passed a reference to the
array, a change to its contents in the called
method will change it in the calling method.
20 Arrays as Parameters
public void passArray() String food
"apple", "fig", "banana", "orange"
System.out.println(countShortWords(food)) priv
ate int countShortWords(String words) int
shortWords 0 for (int counter 0 counter
lt words.length counter) if
(wordscounter.length() lt 4)
shortWords return shortWords
... contd.
21Changing the Contents of an Array in a Called
Method
- public void changePassedArray()
-
- String food "apple", "fig", "banana",
- "orange"
- changeShortWords(food)
- displayStringArray(food)
-
- private void changeShortWords(String words)
-
- for (int counter 0 counter lt words.length
- counter)
- if (wordscounter.length() lt 4)
- wordscounter
- wordscounter.toUpperCase()
-
-