Presentation is loading. Please wait.

Presentation is loading. Please wait.

© A+ Computer Science - www.apluscompsci.com Which of the following statements assigns the letter S to the third row and first column of a two-dimensional.

Similar presentations


Presentation on theme: "© A+ Computer Science - www.apluscompsci.com Which of the following statements assigns the letter S to the third row and first column of a two-dimensional."— Presentation transcript:

1 © A+ Computer Science - www.apluscompsci.com Which of the following statements assigns the letter S to the third row and first column of a two-dimensional array named letterGrid (assuming row-major order). Answer a: letterGrid[3][1] = "S"; Answer b: letterGrid[1][3] = "S"; Answer c: letterGrid[0][2] = "S"; Answer d: letterGrid[2][0] = "S"; Answer e: letterGrid.setValue(2,0,"S");

2 © A+ Computer Science - www.apluscompsci.com

3 Consider the following code segment: ArrayList list; list = new ArrayList (); list.add(new Integer(1)); list.add(new Integer(2)); list.add(new Integer(3)); list.set(2, new Integer(4)); list.add(2, new Integer(5)); l ist.add(new Integer(6)); System.out.println(list); What is printed as a result of executing the code segment?

4 © A+ Computer Science - www.apluscompsci.com Arraylist is a class that houses an array. An ArrayList can store any type. All ArrayLists store the first reference at spot / index position 0.

5 © A+ Computer Science - www.apluscompsci.com 0 1 2 3 4 5 6 7 8 9 nums0000000000 int[] nums = new int[10]; //Java int array An array is a group of items all of the same type which are accessed through a single identifier.

6 © A+ Computer Science - www.apluscompsci.com list nothing null ArrayList list; list is a reference to an ArrayList.

7 © A+ Computer Science - www.apluscompsci.com 0x213 new ArrayList(); ArrayLists are Objects. []

8 © A+ Computer Science - www.apluscompsci.com list 0x213 ArrayList list = new ArrayList(); list is a reference to an ArrayList. []

9 © A+ Computer Science - www.apluscompsci.com List ray = new ArrayList(); ray.add("hello"); ray.add("whoot"); ray.add("contests"); out.println(((String)ray.get(0)).charAt(0)); out.println(((String)ray.get(2)).charAt(0)); ray stores Object references. OUTPUT h c

10 © A+ Computer Science - www.apluscompsci.com

11 With Java 5, you can now specify which type of reference you want to store in the ArrayList. ArrayList words; words = new ArrayList (); List decNums; decnums = new ArrayList ();

12 © A+ Computer Science - www.apluscompsci.com With Java 5, you can now specify which type of reference you want to store in the ArrayList. ArrayList bigStuff; bigStuff = new ArrayList (); List itList; itList = new ArrayList ();

13 © A+ Computer Science - www.apluscompsci.com List ray; ray = new ArrayList (); ray.add("hello"); ray.add("whoot"); ray.add("contests"); out.println(ray.get(0).charAt(0)); out.println(ray.get(2).charAt(0)); ray stores String references. OUTPUT h c

14 © A+ Computer Science - www.apluscompsci.com

15 ArrayList frequently used methods NameUse add(item)adds item to the end of the list add(spot,item)adds item at spot – shifts items up-> set(spot,item)put item at spot z[spot]=item get(spot)returns the item at spot return z[spot] size()returns the # of items in the list remove()removes an item from the list clear()removes all items from the list import java.util.ArrayList;

16 © A+ Computer Science - www.apluscompsci.com ArrayList words; words = new ArrayList (); words.add(0,"it"); words.add("is"); words.add("a"); words.add("lie"); out.println(words); OUTPUT [it, is, a, lie]

17 © A+ Computer Science - www.apluscompsci.com ArrayList nums; nums = new ArrayList (); nums.add(34); nums.add(0,99); nums.add(21); nums.add(11); out.println(nums); OUTPUT [99, 34, 21, 11]

18 © A+ Computer Science - www.apluscompsci.com ArrayList ray; ray = new ArrayList (); ray.add(23); ray.add(11); ray.set(0,66); ray.add(53); ray.set(1,93); ray.add(22); out.println(ray); OUTPUT [66, 93, 53, 22]

19 © A+ Computer Science - www.apluscompsci.com for (int i=0; i<ray.size(); i++) { out.println(ray.get(i)); }.size( ) returns the number of elements/items/spots/boxes or whatever you want to call them.

20 © A+ Computer Science - www.apluscompsci.com ArrayList ray; ray = new ArrayList (); ray.add(23); ray.add(11); ray.add(12); ray.add(65); out.println(ray.get(0)); out.println(ray.get(3)); OUTPUT 23 65.get(spot) returns the reference stored at spot !

21 © A+ Computer Science - www.apluscompsci.com ArrayList ray; ray = new ArrayList (); ray.add(23); ray.add(11); ray.add(12); ray.add(65); for(int i=0; i<ray.size(); i++) out.println(ray.get(i)); OUTPUT 23 11 12 65.get(spot) returns the reference stored at spot !

22 © A+ Computer Science - www.apluscompsci.com

23 ArrayList ray; ray = new ArrayList (); ray.add("a"); ray.add("b"); ray.remove(0); ray.add("c"); ray.add("d"); ray.remove(0); out.println(ray); OUTPUT [c, d]

24 © A+ Computer Science - www.apluscompsci.com ArrayList ray; ray = new ArrayList (); ray.add("a"); ray.add("b"); ray.remove("a"); ray.add("c"); ray.add("d"); ray.remove("d"); out.println(ray); OUTPUT [b, c]

25 © A+ Computer Science - www.apluscompsci.com ArrayList ray; ray = new ArrayList (); ray.add(23); ray.add(11); ray.add(53); for(int num : ray){ out.println(num); } OUTPUT 23 11 53


Download ppt "© A+ Computer Science - www.apluscompsci.com Which of the following statements assigns the letter S to the third row and first column of a two-dimensional."

Similar presentations


Ads by Google