170 likes | 309 Views
CMSC 150 Arrays. CS 150: Fri 10 Feb 2012 . Motivation. Consider a list of your contact email addresses: String emailZero = “ barack@whitehouse.gov ”; String emailOne = “ tweedy@wilco.net ”; String emailTwo = “alec@30rock.com”; String emailThree = “ kwiig@snl.nbc.com ”;
E N D
CMSC 150Arrays CS 150: Fri 10 Feb 2012
Motivation • Consider a list of your contact email addresses: String emailZero = “barack@whitehouse.gov”; String emailOne = “tweedy@wilco.net”; String emailTwo = “alec@30rock.com”; String emailThree = “kwiig@snl.nbc.com”; String emailFour = “lol@icanhazcheezburger.com”; … String emailEightySix = “chadOchoCinco@nfl.com”; … String emailEightSixSevenFiveThreeOhNine = “jenny@tommytutone.com”;
Array • data structure to store multiples of same type of data • can be primitive or class type • Syntax: • type[] variableName; • type[]variableName = new type[size]; • Examples: int[] cardNumbers = new int[10]; boolean[] missingTeeth = new boolean[32]; String[] contacts = new String[8675310];
In Memory cardNumbers null public class ArrayExample { public static void main(String[] args) { int[] cardNumbers; cardNumbers = new int[3]; cardNumbers[0] = 4124201211112222; cardNumbers[1] = 4124201222223333; cardNumbers[2] = 4124201244445555; } }
In Memory cardNumbers 0x12AB79 public class ArrayExample { public static void main(String[] args) { int[] cardNumbers; cardNumbers = new int[3]; cardNumbers[0] = 4124201211112222; cardNumbers[1] = 4124201222223333; cardNumbers[2] = 4124201244445555; } } [0] [1] [2]
In Memory cardNumbers 0x12AB79 public class ArrayExample { public static void main(String[] args) { int[] cardNumbers; cardNumbers = new int[3]; cardNumbers[0] = 4124201211112222; cardNumbers[1] = 4124201222223333; cardNumbers[2] = 4124201244445555; } } [0] 4124201211112222 [1] [2]
In Memory cardNumbers 0x12AB79 public class ArrayExample { public static void main(String[] args) { int[] cardNumbers; cardNumbers = new int[3]; cardNumbers[0] = 4124201211112222; cardNumbers[1] = 4124201222223333; cardNumbers[2] = 4124201244445555; } } [0] 4124201211112222 [1] 4124201222223333 [2]
In Memory cardNumbers 0x12AB79 public class ArrayExample { public static void main(String[] args) { int[] cardNumbers; cardNumbers = new int[3]; cardNumbers[0] = 4124201211112222; cardNumbers[1] = 4124201222223333; cardNumbers[2] = 4124201244445555; } } [0] 4124201211112222 [1] 4124201222223333 [2] 4124201244445555
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 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’); } } [0] null [1] null
In Memory strings 0x33DB20 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’); } } [0] 0x33DB26 [1] null ‘Z’ myFirstChar ‘a’ mySecondChar 2 myLength char chartAt(int index) int length() void printString() …
In Memory strings 0x33DB20 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’); } } [0] 0x33DB26 0x33DC01 [1] ‘Z’ myFirstChar ‘a’ mySecondChar 2 myLength char chartAt(int index) int length() void printString() …
The length field • A variable (not method!) indicates array length int[] cardNumbers = new int[10]; boolean[] missingTeeth = new boolean[32]; String[] contacts = new String[8675310]; inthowManyCards = cardNumbers.length; // 10 inthillbillyFactor = missingTeeth.length; // 32 intnumContacts = contacts.length; // 8675310
The length field • A variable (not method!) indicates array length int[] cardNumbers = new int[10]; boolean[] missingTeeth = new boolean[32]; String[] contacts = new String[8675310]; inthowManyCards = cardNumbers.length; // 10 inthillbillyFactor = missingTeeth.length; // 32 intnumContacts = contacts.length; // 8675310 inthowManyCards = cardNumbers.length(); inthillbillyFactor = missingTeeth.length(); intnumContacts = contacts.length(); Will not compile! For arrays, length is not a method.
Managing Arrays • Typically use for loops: int[] randomNumbers = int[1024]; for (inti = 0; i < randomNumbers.length; i++) { randomNumbers[i] = generator.nextInt(10000); }
Managing Arrays • Typically use for loops: int[] randomNumbers = int[1024]; for (inti = 0; i < randomNumbers.length; i++) { randomNumbers[i] = generator.nextInt(10000); } intnumberOfOdds= 0; for (inti = 0; i < randomNumbers.length; i++) { if ( randomNumbers[i] % 2 != 0 ) { numberOfOdds++; } }