CMSC 150 ARRAYS CS 150: Fri 10 Feb 2012
Motivation Consider a list of your contact addresses: String Zero = String One = String Two = String Three = String Four = … String EightySix = … String EightSixSevenFiveThreeOhNine =
Array data structure to store multiples of same type of data can be primitive or class type S yntax: type[] variableName; type[] variableName = new type[size]; Examples: int[] cardNumbers = new int[10]; boolean[] missingTeeth = new boolean[32]; String[] contacts = new String[ ];
public class ArrayExample { public static void main(String[] args) { int[] cardNumbers; cardNumbers = new int[3]; cardNumbers[0] = ; cardNumbers[1] = ; cardNumbers[2] = ; } In Memory cardNumbers null
public class ArrayExample { public static void main(String[] args) { int[] cardNumbers; cardNumbers = new int[3]; cardNumbers[0] = ; cardNumbers[1] = ; cardNumbers[2] = ; } In Memory cardNumbers 0x12AB79 [0] [1] [2]
public class ArrayExample { public static void main(String[] args) { int[] cardNumbers; cardNumbers = new int[3]; cardNumbers[0] = ; cardNumbers[1] = ; cardNumbers[2] = ; } In Memory cardNumbers 0x12AB79 [0] [1] [2]
public class ArrayExample { public static void main(String[] args) { int[] cardNumbers; cardNumbers = new int[3]; cardNumbers[0] = ; cardNumbers[1] = ; cardNumbers[2] = ; } In Memory cardNumbers 0x12AB79 [0] [1] [2]
public class ArrayExample { public static void main(String[] args) { int[] cardNumbers; cardNumbers = new int[3]; cardNumbers[0] = ; cardNumbers[1] = ; cardNumbers[2] = ; } In Memory cardNumbers 0x12AB79 [0] [1] [2]
In Memory strings null public class ArrayExample { public static void main(String[] args) { SimpleString[] strings; strings = new SimpleString[2]; strings[0] = new SimpleString(‘Z’,’a’); strings[1] = new SimpleString(‘O’,’y’); }
In Memory strings 0x33DB20 [0] [1] null public class ArrayExample { public static void main(String[] args) { SimpleString[] strings; strings = new SimpleString[2]; strings[0] = new SimpleString(‘Z’,’a’); strings[1] = new SimpleString(‘O’,’y’); }
In Memory strings 0x33DB20 [0] [1] null mySecondChar myLength myFirstChar ‘Z’ ‘a’ 2 char chartAt(int index) int length() void printString() … public class ArrayExample { public static void main(String[] args) { SimpleString[] strings; strings = new SimpleString[2]; strings[0] = new SimpleString(‘Z’,’a’); strings[1] = new SimpleString(‘O’,’y’); } 0x33DB26
In Memory strings 0x33DB20 [0] [1] mySecondChar myLength myFirstChar ‘Z’ ‘a’ 2 char chartAt(int index) int length() void printString() … public class ArrayExample { public static void main(String[] args) { SimpleString[] strings; strings = new SimpleString[2]; strings[0] = new SimpleString(‘Z’,’a’); strings[1] = new SimpleString(‘O’,’y’); } 0x33DB26 0x33DC01
The length field A variable (not method!) indicates array length int[] cardNumbers = new int[10]; boolean[] missingTeeth = new boolean[32]; String[] contacts = new String[ ]; int howManyCards = cardNumbers.length; // 10 int hillbillyFactor = missingTeeth.length; // 32 int numContacts = contacts.length; //
The length field A variable (not method!) indicates array length int[] cardNumbers = new int[10]; boolean[] missingTeeth = new boolean[32]; String[] contacts = new String[ ]; int howManyCards = cardNumbers.length; // 10 int hillbillyFactor = missingTeeth.length; // 32 int numContacts = contacts.length; // int howManyCards = cardNumbers.length(); int hillbillyFactor = missingTeeth.length(); int numContacts = contacts.length(); Will not compile! For arrays, length is not a method. Will not compile! For arrays, length is not a method.
Managing Arrays Typically use for loops: int[] randomNumbers = int[1024]; for (int i = 0; i < randomNumbers.length; i++) { randomNumbers[i] = generator.nextInt(10000); }
Managing Arrays Typically use for loops: int[] randomNumbers = int[1024]; for (int i = 0; i < randomNumbers.length; i++) { randomNumbers[i] = generator.nextInt(10000); } int numberOfOdds = 0; for (int i = 0; i < randomNumbers.length; i++) { if ( randomNumbers[i] % 2 != 0 ) { numberOfOdds++; }
Let’s Try Some…