Arrays Arrays What good are they Arrays are

  • Slides: 39
Download presentation
Arrays

Arrays

Arrays – What good are they? § § § Arrays are a list of

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

Arrays § Declaration § § Initialization § § <data type>[ ] <name>; <name> =

Arrays § Declaration § § Initialization § § <data type>[ ] <name>; <name> = new <data type>[<size>]; Data type can be primitive or reference § § char[ ] alphabet; alphabet = new char[26];

Arrays § § Arrays are objects in java They have methods like to. String(

Arrays § § Arrays are objects in java They have methods like to. String( ) 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’};

Arrays § What does this look like in memory?

Arrays § What does this look like in memory?

Arrays § What does this look like in memory? alphabet a b c d

Arrays § What does this look like in memory? alphabet a b c d e f g : : y z

Arrays § How do we index (access) the individual ellements? alphabet a b c

Arrays § How do we index (access) the individual ellements? alphabet a b c d e f g : : y z

Arrays § How do we index (access) the individual ellements? alphabet[0] a b c

Arrays § How do we index (access) the individual ellements? alphabet[0] a b c d e f g : : y z

Arrays § How do we index (access) the individual ellements? alphabet[0] alphabet[1] a b

Arrays § How do we index (access) the individual ellements? alphabet[0] alphabet[1] a b c d e f g : : y z

Arrays § How do we index (access) the individual ellements? alphabet[0] alphabet[1] alphabet[25] a

Arrays § How do we index (access) the individual ellements? alphabet[0] alphabet[1] alphabet[25] a b c d e f g : : y z

Arrays § These are the indices alphabet[0] alphabet[1] alphabet[25] a b c d e

Arrays § These are the indices alphabet[0] alphabet[1] alphabet[25] a b c d e f g : : y z

Arrays § The index to an array can be any expression as long as

Arrays § The index to an array can be any expression as long as it is within the valid range: § § § alphabet[0] alphabet[2*7] alphabet[x*y+2] alphabet[(int) Math. rand()*25] The only problem occurs if the index is § § Less than 0 Greater than or equal to the length

Arrays – What good are they? § Example: int[ ] test. Grade = new

Arrays – What good are they? § Example: int[ ] test. Grade = new int[9]; for(int i = 0; i < test. Grade. length; i++) test. Grade[i] = in. Box. get. Integer( “Enter test score for student” + i); Notice length is a public data member, not a method!

Arrays – What good are they? § Example: double average = 0; for(int i

Arrays – What good are they? § Example: double average = 0; for(int i = 0; i < test. Grade. length; i++) average += test. Grade[i]; average = average / test. Grade. length; System. out. println(“Test Average: ” + average); Notice length is a public data member, not a method!

Arrays – What good are they? § Questions? ? ?

Arrays – What good are they? § Questions? ? ?

Arrays – Objects § § Arrays of Objects are just as useful as arrays

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 String[9]; String[ ] names = {“Katie”, “Andy”, “Gil”};

Arrays – Objects § Using: § § § String[ ] names = new String[9];

Arrays – Objects § Using: § § § String[ ] names = new String[9]; Initially, all elements are null Each element must be defined

Arrays – Objects § String[ ] names = new String[9]; names null null null

Arrays – Objects § String[ ] names = new String[9]; names null null null

Arrays – Objects § String[ ] names = new String[9]; names[0] = new String(“Gil”);

Arrays – Objects § String[ ] names = new String[9]; names[0] = new String(“Gil”); names[0] Gil null null

Arrays – Objects § String[ ] names = new String[9]; names[0] = new String(“Gil”);

Arrays – Objects § String[ ] names = new String[9]; names[0] = new String(“Gil”); names[8] = “Phil”; names[8] Gil null null Phil

Arrays – Objects § String[ ] names = new String[9]; names[0] = new String(“Gil”);

Arrays – Objects § String[ ] names = new String[9]; names[0] = new String(“Gil”); names[8] = “Phil”; for(int i=1; i<8; i++) names[i] = alphabet[i] + “il”; names[8] Gil null null bil Phil

Arrays – Objects § String[ ] names = new String[9]; names[0] = new String(“Gil”);

Arrays – Objects § String[ ] names = new String[9]; names[0] = new String(“Gil”); names[8] = “Phil”; for(int i=1; i<8; i++) names[i] = alphabet[i] + “il”; names Gil names[8] null null bil cil Phil

Arrays – Objects § String[ ] names = new String[9]; names[0] = new String(“Gil”);

Arrays – Objects § String[ ] names = new String[9]; names[0] = new String(“Gil”); names[8] = “Phil”; for(int i=1; i<8; i++) names[i] = alphabet[i] + “il”; This would eventually initialize all the elements. names Gil names[8] null null bil cil Phil

Arrays – Objects § § Elements must be initialized before they can be used

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: § § names[1]. equals(names[7]); System. out. println(names[5] + “received a grade of “ + alphabet[0]);

Arrays – Objects § Questions? ? ?

Arrays – Objects § Questions? ? ?

Arrays § § Exercise: Write a code segment that: § § declares a String

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 Input. Box) Prints the string with the smallest length Prints the string with the max length

Arrays § § § Removing objects from arrays: You can simply replace it with

Arrays § § § Removing objects from arrays: You can simply replace it with a new object names[0] = new String(“Gil 2”); names[0] Gil null null Gil 2

Arrays § Also like other objects, two elements can point to the same object

Arrays § Also like other objects, two elements can point to the same object § names[0] = “Moe”; names[1] = names[0]; § names[0] Moe null null

Arrays § § § Just like regular objects, you can pass and return arrays

Arrays § § § Just like regular objects, you can pass and return arrays to and from methods example. method 1(squares); public void method 1(int[ ] pattern){ // do something here } squares 1 2 4 9 16 25 36 49 64

Arrays § § § Just like regular objects, you can pass and return arrays

Arrays § § § Just like regular objects, you can pass and return arrays to and from methods example. method 1(squares); public void method 1(int[ ] pattern){ // do something here } squares pattern 1 2 4 9 16 25 36 49 64

Arrays § § § Just like regular objects, you can pass and return arrays

Arrays § § § Just like regular objects, you can pass and return arrays to and from methods example. method 1(squares); public void method 1(int[ ] pattern){ // do something here } pattern[0] == squares[0] true pattern[1] == squares[1] true pattern ? = squares pattern 1 2 4 9 16 25 36 49 64

Arrays § § § Just like regular objects, you can pass and return arrays

Arrays § § § Just like regular objects, you can pass and return arrays to and from methods example. method 1(squares); public void method 1(int[ ] pattern){ // do something here } pattern[0] == squares[0] true pattern[1] == squares[1] true pattern == squares true squares pattern 1 2 4 9 16 25 36 49 64

Arrays § Just like regular objects, you can pass and return arrays to and

Arrays § Just like regular objects, you can pass and return arrays to and from methods § example. method 1(names); § public void method 1(String[ ] people){ // do something here } names null null Moe

Arrays § Just like regular objects, you can pass and return arrays to and

Arrays § Just like regular objects, you can pass and return arrays to and from methods § example. method 1(names); § public void method 1(String[ ] people){ // do something here } names people null null Moe

Arrays § Just like regular objects, you can pass and return arrays to and

Arrays § Just like regular objects, you can pass and return arrays to and from methods § example. method 1(names); § public void method 1(String[ ] people){ // do something here } people[1]. equals(names[1]) people[1] == names[1] people == names people null null Moe

Arrays § Just like regular objects, you can pass and return arrays to and

Arrays § Just like regular objects, you can pass and return arrays to and from methods § example. method 1(names); § public void method 1(String[ ] people){ // do something here } people[1]. equals(names[1]) true people[1] == names[1] people == names people null null Moe

Arrays § Just like regular objects, you can pass and return arrays to and

Arrays § Just like regular objects, you can pass and return arrays to and from methods § example. method 1(names); § public void method 1(String[ ] people){ // do something here } people[1]. equals(names[1]) true people[1] == names[1] true people == names people null null Moe

Arrays § Just like regular objects, you can pass and return arrays to and

Arrays § Just like regular objects, you can pass and return arrays to and from methods § example. method 1(names); § public void method 1(String[ ] people){ // do something here } people[1]. equals(names[1]) true people[1] == names[1] true people == names true names people null null Moe

Arrays § Questions? ?

Arrays § Questions? ?