1 / 21

Wrapper Classes and ArrayList

Wrapper Classes and ArrayList. Mrs. C. Furman 9/15/2008. Wrapper Classes . Used to make primitive data into objects! We are using Integer to make ints objects We are using Double to make doubles objects.

juana
Download Presentation

Wrapper Classes and ArrayList

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. Wrapper Classes and ArrayList Mrs. C. Furman 9/15/2008

  2. Wrapper Classes • Used to make primitive data into objects! • We are using Integer to make ints 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!!

  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); // Create an object of type Double to store 1.5 double d = myDouble.doubleValue();

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

  6. ArrayList Methods

  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 () 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 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 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<Integer> ray; ray = new ArrayList<Integer>(); 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<String> ray; ray = new ArrayList<String>(); 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

  19. Array Vs. ArrayList GCOC – APCS A

  20. For Each Loops • 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. • 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 }

More Related