1 / 17

CMSC 150 Arrays

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 ”;

sherri
Download Presentation

CMSC 150 Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMSC 150Arrays 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 • Syntax: • type[] variableName; • type[]variableName = new type[size]; • Examples: int[] cardNumbers = new int[10]; boolean[] missingTeeth = new boolean[32]; String[] contacts = new String[8675310];

  4. 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; } }

  5. 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]

  6. 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]

  7. 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]

  8. 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

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

  11. 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() …

  12. 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() …

  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]; inthowManyCards = cardNumbers.length; // 10 inthillbillyFactor = missingTeeth.length; // 32 intnumContacts = 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]; 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.

  15. Managing Arrays • Typically use for loops: int[] randomNumbers = int[1024]; for (inti = 0; i < randomNumbers.length; i++) { randomNumbers[i] = generator.nextInt(10000); }

  16. 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++; } }

  17. Let’s Try Some…

More Related