Arrays - PowerPoint PPT Presentation

About This Presentation
Title:

Arrays

Description:

g. z. alphabet. Arrays. How do we index (access) the individual ... Prints the string with the smallest length. Prints the string with the max length. Arrays ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 40
Provided by: mccl8
Category:

less

Transcript and Presenter's Notes

Title: Arrays


1
  • Arrays

2
Arrays 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

3
Arrays
  • Declaration
  • ltdata typegt ltnamegt
  • Initialization
  • ltnamegt new ltdata typegtltsizegt
  • Data type can be primitive or reference
  • char alphabet
  • alphabet new char26

4
Arrays
  • 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

5
Arrays
  • What does this look like in memory?

6
Arrays
  • What does this look like in memory?

alphabet
a
b
c
d
e
f
g


y
z
7
Arrays
  • How do we index (access) the individual ellements?

alphabet
a
b
c
d
e
f
g


y
z
8
Arrays
  • How do we index (access) the individual ellements?

alphabet
a
b
c
alphabet0
d
e
f
g


y
z
9
Arrays
  • How do we index (access) the individual ellements?

alphabet
a
b
c
alphabet0
d
e
alphabet1
f
g


y
z
10
Arrays
  • How do we index (access) the individual ellements?

alphabet
a
b
c
alphabet0
d
e
alphabet1
f
g

alphabet25

y
z
11
Arrays
  • These are the indices

alphabet
a
b
c
alphabet0
d
e
alphabet1
f
g

alphabet25

y
z
12
Arrays
  • 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

13
Arrays 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!

14
Arrays 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!

15
Arrays What good are they?
  • Questions???

16
Arrays 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

17
Arrays Objects
  • Using
  • String names new String9
  • Initially, all elements are null
  • Each element must be defined

18
Arrays Objects
  • String names new String9

names
null
null
null
null
null
null
null
null
null
19
Arrays Objects
  • String names new String9
  • names0 new String(Gil)

names
Gil
null
names0
null
null
null
null
null
null
null
20
Arrays Objects
  • String names new String9
  • names0 new String(Gil)
  • names8 Phil

names
Gil
null
names8
null
null
null
null
null
null
Phil
21
Arrays 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
22
Arrays 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
23
Arrays 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
24
Arrays 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)

25
Arrays Objects
  • Questions???

26
Arrays
  • 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

27
Arrays
  • 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
28
Arrays
  • 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
29
Arrays
  • 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
30
Arrays
  • 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
31
Arrays
  • 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
32
Arrays
  • 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
33
Arrays
  • 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
34
Arrays
  • 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
35
Arrays
  • 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
36
Arrays
  • 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
37
Arrays
  • 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
38
Arrays
  • 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
39
Arrays
  • Questions????
Write a Comment
User Comments (0)
About PowerShow.com