Presentation is loading. Please wait.

Presentation is loading. Please wait.

Wrapper Classes and ArrayList Mrs. C. Furman 9/15/2008.

Similar presentations


Presentation on theme: "Wrapper Classes and ArrayList Mrs. C. Furman 9/15/2008."— Presentation transcript:

1 Wrapper Classes and ArrayList Mrs. C. Furman 9/15/2008

2 Wrapper Classes Used to make primitive data into objects! Used to make primitive data into objects! We are using Integer to make ints objects We are using Integer to make ints objects We are using Double to make doubles objects. We are using Double to make doubles objects. We primarily we are going to use this with ArrayList, because we can only store objects in an ArrayList!! We primarily we are going to use this with ArrayList, because we can only store objects in an ArrayList!!

3 Integer Class Integer myInt = new Integer (3); //declares and creates myInt as an Integer, and stores 3. int i = myInt.intValue(); //unwrapped the 3, and assign it to i.

4 Double Class Double myDouble = new Double (1.5); Double myDouble = new Double (1.5); // Create an object of type Double to store 1.5 double d = myDouble.doubleValue();

5 ArrayList Can only store OBJECTS!!! Can only store OBJECTS!!! Size is not a constant, we can resize as we go. Size is not a constant, we can resize as we go. inserts into the ArrayList easily, deletes from ArrayList inserts into the ArrayList easily, deletes from ArrayList

6 ArrayList Methods NameUse size() Returns the number of elements in the List add(Object) Appends an Object to the end of the List add (index, Object) Inserts an Object into the List at the given index. Every other object is shifted down. get (index) Returns the Object at the given index set (index, Object) Replaces the Object at the given index with the new Object remove(index) Removes the Object at the index, and shifts all Objects to the right down 1.

7 Constructing an ArrayList ArrayList myList = new ArrayList(); boolean add(Object o) boolean tf = myList.add(new Integer(3)); boolean tf = myList.add(new Integer (5)); Elements are added to the end of the list… 3 5

8 Add all the values in the ArrayList int sum = 0; Integer myInt; for (int i = 0; i<myList.size(); i++) { myInt = ((Integer)(myList.get(i)).intValue(); sum += myInt; }

9 Add Integer Objects int sum = 0; Integer i = new Integer (4); Integer j = new Integer (6); sum = i + j;

10 get(i) The return type of get is an Object.. so you need to typecast to get it to the correct type. myInt = ((Integer)(myList.get(i)).intValue(); typecast ArrayList method (parameter) method () typecast ArrayList method (parameter) method () ArrayList Integer Store multiple types in an ArrayList…

11 .add(index, obj) void add(int index, E obj) myList.add(0, new Integer (7)); 7 3 5 We can add to our list at the index one more that what we currently have, and it will resize, if we try to add well beyond that… myList.add (15, new Integer (15)); it will error.

12 Inserting into an ArrayList 3 5 7 10 9 9 11 3 5 7 10 9 9 11 myList.add (4, new Integer (13)); 3 5 7 10 13 9 9 11

13 .add(Object) Appends the object to the end of the list. Example: 3 5 7 10 9 9 11 myList.add ( new Integer (13)); 3 5 7 10 9 9 11 13

14 .remove (index) 3 5 7 10 9 9 11 3 5 7 10 9 9 11 Integer myInt = myList.remove (4); //removes 10 from the list…stores 10 in myInt 3 5 7 9 9 11

15 .set(index, Object) 3 5 7 10 9 9 11 Integer myInt = myList.set (4, new Integer(8)); //8 is add to the list, 10 is stored in myInt 3 5 7 8 9 9 11

16 Example 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); System.out.println(ray); Output: [66, 93, 53, 22]

17 Example 2 ArrayList ray; ray = new ArrayList (); ray.add("a"); ray.add("b"); ray.remove(0); ray.add("c"); ray.add("d"); ray.remove(0); System.out.println(ray); Output: [c, d ]

18 Array Vs. ArrayList GCOC – APCS A ArrayListArray ArrayList myList = new ArrayList (); String [ ] myList = new String[2]; String a = new String(“woohoo”); myList.add(a); String a = new String(“woohoo”); myList[0] = a; String b = new String(“Frog”); myList.add(b); String b = new String(“Frog”); myList[1] = b;

19 Array Vs. ArrayList GCOC – APCS A ArrayListArray int theSize = myList.size();int theSize = myList.length; Object o = myList.get(1); -or- String o = (String )myList.get(1); String o = myList[1]; myList.remove(1);myList[1] = null;

20 For Each Loops For Each loops are great to use with Arrays, ArrayList, 2D Arrays, etc. For Each loops are great to use with Arrays, ArrayList, 2D Arrays, etc. Use whenever you want to loop from the beginning to the end of a list. Use whenever you want to loop from the beginning to the end of a list. Do not use if you do not want to search the entire list. Do not use if you do not want to search the entire list.

21 For Each Structure for (Object o : arrayName) { Type of Object stored Name of the collection } Example: For Each int[] data = {3, 5, 7}; int sum = 0; for (int x: data) { sum += x; } Example: Regular For int[] data = {3, 5, 7}; int sum = 0; for (int i = 0; i < data.length; i++) { sum += data[i]; sum += data[i];}


Download ppt "Wrapper Classes and ArrayList Mrs. C. Furman 9/15/2008."

Similar presentations


Ads by Google