Download presentation
Presentation is loading. Please wait.
Published byAnnabel Lewis Modified over 9 years ago
1
CMSC 150 ARRAYS CS 150: Fri 10 Feb 2012
2
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”;
3
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[8675310];
4
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 null
5
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 [0] [1] [2]
6
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 [0] [1] [2] 4124201211112222
7
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 [0] [1] [2] 4124201211112222 4124201222223333
8
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 [0] [1] [2] 4124201211112222 4124201222223333 4124201244445555
9
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’); }
10
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’); }
11
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
12
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
13
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]; int howManyCards = cardNumbers.length; // 10 int hillbillyFactor = missingTeeth.length; // 32 int numContacts = contacts.length; // 8675310
14
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]; int howManyCards = cardNumbers.length; // 10 int hillbillyFactor = missingTeeth.length; // 32 int numContacts = contacts.length; // 8675310 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.
15
Managing Arrays Typically use for loops: int[] randomNumbers = int[1024]; for (int i = 0; i < randomNumbers.length; i++) { randomNumbers[i] = generator.nextInt(10000); }
16
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++; }
17
Let’s Try Some…
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.