Upload
willa-hunter
View
234
Download
1
Embed Size (px)
Citation preview
Wrapper Classes and Wrapper Classes and ArrayListArrayList
Mrs. C. FurmanMrs. C. Furman
9/15/20089/15/2008
Wrapper ClassesWrapper Classes
Used to make primitive data into objects!Used to make primitive data into objects! We are using Integer to make ints objectsWe 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 We primarily we are going to use this with
ArrayList, because we can only store objects ArrayList, because we can only store objects in an ArrayList!!in an ArrayList!!
Integer ClassInteger Class
Integer myInt = new Integer (3); //declares and Integer myInt = new Integer (3); //declares and creates myInt as an Integer, and stores 3.creates myInt as an Integer, and stores 3.
int i = myInt.intValue(); //unwrapped the 3, and int i = myInt.intValue(); //unwrapped the 3, and assign it to i.assign it to i.
Double ClassDouble Class
Double myDouble = new Double (1.5); Double myDouble = new Double (1.5); // Create an object of type Double to store 1.5// Create an object of type Double to store 1.5
double d = myDouble.doubleValue();double d = myDouble.doubleValue();
ArrayListArrayList
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 inserts into the ArrayList easily, deletes from
ArrayListArrayList
ArrayList MethodsArrayList MethodsNameName UseUse
size()size() Returns the number of elements Returns the number of elements in the Listin the List
add(Object)add(Object) Appends an Object to the end of Appends an Object to the end of the Listthe List
add (index, Object)add (index, Object) Inserts an Object into the List at Inserts an Object into the List at the given index. Every other the given index. Every other object is shifted down.object is shifted down.
get (index)get (index) Returns the Object at the given Returns the Object at the given indexindex
set (index, Object)set (index, Object) Replaces the Object at the given Replaces the Object at the given index with the new Objectindex with the new Object
remove(index)remove(index) Removes the Object at the index, Removes the Object at the index, and shifts all Objects to the right and shifts all Objects to the right down 1.down 1.
Constructing an ArrayListConstructing an ArrayList
ArrayList myList = new ArrayList();ArrayList myList = new ArrayList();
boolean add(Object o)boolean add(Object o)boolean tf = myList.add(new Integer(3));boolean tf = myList.add(new Integer(3));boolean tf = myList.add(new Integer (5));boolean tf = myList.add(new Integer (5));
Elements are added to the end of the list… Elements are added to the end of the list… 3 53 5
Add all the values in the ArrayListAdd all the values in the ArrayList
int sum = 0;int sum = 0;
Integer myInt;Integer myInt;
for (int i = 0; i<myList.size(); i++)for (int i = 0; i<myList.size(); i++)
{{
myInt = ((Integer)(myList.get(i)).intValue();myInt = ((Integer)(myList.get(i)).intValue();
sum += myInt;sum += myInt;
}}
Add Integer ObjectsAdd Integer Objects
int sum = 0;int sum = 0;
Integer i = new Integer (4);Integer i = new Integer (4);
Integer j = new Integer (6);Integer j = new Integer (6);
sum = i + j;sum = i + j;
get(i)get(i)The return type of get is an Object.. so you need to The return type of get is an Object.. so you need to
typecast to get it to the correct type.typecast to get it to the correct type.
myInt = ((Integer)(myList.get(i)).intValue();myInt = ((Integer)(myList.get(i)).intValue();
typecast ArrayList method (parameter) method ()typecast ArrayList method (parameter) method ()
ArrayList IntegerArrayList Integer
Store multiple types in an ArrayList… Store multiple types in an ArrayList…
.add(index, obj).add(index, obj)
void add(int index, E obj)void add(int index, E obj)
myList.add(0, new Integer (7));myList.add(0, new Integer (7));
7 3 57 3 5
We can add to our list at the index one more that what we 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 currently have, and it will resize, if we try to add well beyond that… that…
myList.add (15, new Integer (15));myList.add (15, new Integer (15));it will error.it will error.
Inserting into an ArrayListInserting into an ArrayList
3 5 7 10 9 9 113 5 7 10 9 9 11
myList.add (4, new Integer (13));myList.add (4, new Integer (13));
3 5 7 10 13 9 9 113 5 7 10 13 9 9 11
.add(Object).add(Object)
Appends the object to the end of the list.Appends the object to the end of the list.
Example:Example:
3 5 7 10 9 9 113 5 7 10 9 9 11myList.add ( new Integer (13));myList.add ( new Integer (13));
3 5 7 10 9 9 11 133 5 7 10 9 9 11 13
.remove (index).remove (index)
3 5 7 10 9 9 113 5 7 10 9 9 11
Integer myInt = myList.remove (4);Integer myInt = myList.remove (4);
//removes 10 from the list…stores 10 in myInt//removes 10 from the list…stores 10 in myInt
3 5 7 9 9 113 5 7 9 9 11
.set(index, Object).set(index, Object)
3 5 7 10 9 9 113 5 7 10 9 9 11
Integer myInt = myList.set (4, new Integer(8));Integer myInt = myList.set (4, new Integer(8));
//8 is add to the list, 10 is stored in myInt//8 is add to the list, 10 is stored in myInt
3 5 7 8 9 9 113 5 7 8 9 9 11
ExampleExample
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]
Example 2Example 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 ]
Array Vs. ArrayList Array Vs. ArrayList
GCOC – APCS A
ArrayList Array
ArrayList<String> myList = new ArrayList<String>();
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;
Array Vs. ArrayList Array Vs. ArrayList
GCOC – APCS A
ArrayList Array
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;
For Each LoopsFor Each Loops
For Each loops are great to use with Arrays, For Each loops are great to use with Arrays, ArrayList, 2D Arrays, etc. ArrayList, 2D Arrays, etc.
Use whenever you want to loop from the Use whenever you want to loop from the beginning to the end of a list.beginning to the end of a list.
Do not use if you do not want to search the Do not use if you do not want to search the entire list.entire list.
For Each StructureFor Each Structureforfor (Object o : arrayName) (Object o : arrayName){ { Type of Object storedType of Object stored Name of the collection Name of the collection
}}
Example: For EachExample: For Each
int[] data = {3, 5, 7};int[] data = {3, 5, 7};
int sum = 0;int sum = 0;
for (int x: data)for (int x: data)
{{
sum += x;sum += x;
}}
Example: Regular ForExample: Regular For
int[] data = {3, 5, 7};int[] data = {3, 5, 7};
int sum = 0;int sum = 0;for (int i = 0; i < data.length; i++)for (int i = 0; i < data.length; i++)
{{
sum += data[i];sum += data[i];
}}