Title: Arrays
1 2Arrays What good are they?
- Arrays are a list of data
- They are very good at storing a number of
variables of the same data type. - These can be of any data type, primitive or
reference -
-
3Arrays
- Declaration
- ltdata typegt ltnamegt
- Initialization
- ltnamegt new ltdata typegtltsizegt
- Data type can be primitive or reference
- char alphabet
- alphabet new char26
4Arrays
- Arrays are objects in java
- They have methods like toString( ) and data
members like length - They are special objects, and like strings have a
number of ways they can be initialized - alphabet a, b, c, , y, z
5Arrays
- What does this look like in memory?
6Arrays
- What does this look like in memory?
alphabet
a
b
c
d
e
f
g
y
z
7Arrays
- How do we index (access) the individual ellements?
alphabet
a
b
c
d
e
f
g
y
z
8Arrays
- How do we index (access) the individual ellements?
alphabet
a
b
c
alphabet0
d
e
f
g
y
z
9Arrays
- How do we index (access) the individual ellements?
alphabet
a
b
c
alphabet0
d
e
alphabet1
f
g
y
z
10Arrays
- How do we index (access) the individual ellements?
alphabet
a
b
c
alphabet0
d
e
alphabet1
f
g
alphabet25
y
z
11Arrays
alphabet
a
b
c
alphabet0
d
e
alphabet1
f
g
alphabet25
y
z
12Arrays
- The index to an array can be any expression as
long as it is within the valid range - alphabet0
- alphabet27
- alphabetxy2
- alphabet(int) Math.rand()25
- The only problem occurs if the index is
- Less than 0
- Greater than or equal to the length
13Arrays What good are they?
- Example
- int testGrade new int9
- for(int i 0 i lt testGrade.length i)
- testGradei inBox.getInteger(
- Enter test score for student i)
- Notice length is a public data member, not a
method!
14Arrays What good are they?
- Example
- double average 0
- for(int i 0 i lt testGrade.length i)
- average testGradei
- average average / testGrade.length
- System.out.println(Test Average average)
- Notice length is a public data member, not a
method!
15Arrays What good are they?
16Arrays Objects
- Arrays of Objects are just as useful as arrays of
primitives - They are declared and initialized the exact same
way - String names new String9
- String names Katie, Andy, Gil
-
17Arrays Objects
- Using
- String names new String9
- Initially, all elements are null
- Each element must be defined
-
18Arrays Objects
names
null
null
null
null
null
null
null
null
null
19Arrays Objects
- String names new String9
- names0 new String(Gil)
-
names
Gil
null
names0
null
null
null
null
null
null
null
20Arrays Objects
- String names new String9
- names0 new String(Gil)
- names8 Phil
names
Gil
null
names8
null
null
null
null
null
null
Phil
21Arrays Objects
- String names new String9
- names0 new String(Gil)
- names8 Phil
- for(int i1 ilt8 i)
- namesi alphabeti il
names
Gil
names8
null
bil
null
null
null
null
null
Phil
22Arrays Objects
- String names new String9
- names0 new String(Gil)
- names8 Phil
- for(int i1 ilt8 i)
- namesi alphabeti il
names
Gil
names8
bil
null
null
null
cil
null
null
Phil
23Arrays Objects
- String names new String9
- names0 new String(Gil)
- names8 Phil
- for(int i1 ilt8 i)
- namesi alphabeti il
This would eventually initialize all the elements.
names
Gil
names8
bil
null
null
null
cil
null
null
Phil
24Arrays Objects
- Elements must be initialized before they can be
used - After an element has been initialized, they can
be used just like regular variables - names1.equals(names7)
- System.out.println(names5
- received a grade of alphabet0)
-
25Arrays Objects
26Arrays
- Exercise
- Write a code segment that
- declares a String array of size 10
- Initializes it with 10 Strings entered by the
user (hint use an InputBox) - Prints the string with the smallest length
- Prints the string with the max length
-
27Arrays
- Removing objects from arrays
- You can simply replace it with a new object
- names0 new String(Gil 2)
names
Gil
null
names0
null
null
null
Gil 2
null
null
null
null
28Arrays
- Also like other objects, two elements can point
to the same object - names0 Moe
- names1 names0
names
Moe
names0
null
null
null
null
null
null
null
29Arrays
- Just like regular objects, you can pass and
return arrays to and from methods - example.method1(squares)
- public void method1(int pattern)
- // do something here
-
squares
1
2
4
9
16
25
36
49
64
30Arrays
- Just like regular objects, you can pass and
return arrays to and from methods - example.method1(squares)
- public void method1(int pattern)
- // do something here
-
squares
1
2
pattern
4
9
16
25
36
49
64
31Arrays
- Just like regular objects, you can pass and
return arrays to and from methods - example.method1(squares)
- public void method1(int pattern)
- // do something here
-
squares
1
2
pattern
4
9
16
25
36
49
pattern0 squares0 ? true pattern1
squares1 ? true pattern ? squares
64
32Arrays
- Just like regular objects, you can pass and
return arrays to and from methods - example.method1(squares)
- public void method1(int pattern)
- // do something here
-
squares
1
2
pattern
4
9
16
25
36
49
pattern0 squares0 ? true pattern1
squares1 ? true pattern squares ? true
64
33Arrays
- Just like regular objects, you can pass and
return arrays to and from methods - example.method1(names)
- public void method1(String people)
- // do something here
-
names
Moe
null
null
null
null
null
null
null
34Arrays
- Just like regular objects, you can pass and
return arrays to and from methods - example.method1(names)
- public void method1(String people)
- // do something here
-
names
Moe
null
people
null
null
null
null
null
null
35Arrays
- Just like regular objects, you can pass and
return arrays to and from methods - example.method1(names)
- public void method1(String people)
- // do something here
-
names
Moe
null
people
null
null
null
null
null
people1.equals(names1) people1
names1 people names
null
36Arrays
- Just like regular objects, you can pass and
return arrays to and from methods - example.method1(names)
- public void method1(String people)
- // do something here
-
names
Moe
null
people
null
null
null
null
null
people1.equals(names1) ? true people1
names1 people names
null
37Arrays
- Just like regular objects, you can pass and
return arrays to and from methods - example.method1(names)
- public void method1(String people)
- // do something here
-
names
Moe
null
people
null
null
null
null
null
people1.equals(names1) ? true people1
names1 ? true people names
null
38Arrays
- Just like regular objects, you can pass and
return arrays to and from methods - example.method1(names)
- public void method1(String people)
- // do something here
-
names
Moe
null
people
null
null
null
null
null
people1.equals(names1) ? true people1
names1 ? true people names ? true
null
39Arrays