ArrayList Notes

100 %
0 %
Information about ArrayList Notes
Education

Published on January 29, 2016

Author: linztillman

Source: authorstream.com

Array List Notes: Array List Notes AP Computer Science Tillman Spring 2015 Array Lists : Array Lists ArrayLists are containers used to store lists of objects. The ArrayList class provides some advantages over the Array class. Array Lists Advantages: Array Lists Advantages You can add an item into an ArrayList and the list expands automatically. You can remove an item and the ArrayList contracts automatically. Array Lists: Array Lists You do need to use an import statement with the ArrayList class. import java.util.ArrayList ; Array Lists: Array Lists To declare an ArrayList object, you can use the default constructor. Default constructor has a capacity of 10 items. ArrayList names = new ArrayList (); Array Lists : Array Lists ArrayList is a descendant of List (which extends Collection ), but because List and Collection are interfaces , you cannot instantiate them. Collection bad = new Collection(); //illegal List ray = new ArrayList(); //legal ArrayList list = new ArrayLis //legal Array Lists: Array Lists You can also specific a capacity if you like. This ArrayList now has a capacity of 2 0 items. If you know will need more than 10 items on the outset, it is more efficient to create an ArrayList with a larger capacity. ArrayList names = new ArrayList (20); Array Lists: Array Lists ArrayList - frequently used methods Name Use add(item) adds item to the end of the list add( index,item ) adds item at index – shifts items up-> set( index,item ) replaces item at index (returns old element) (similar to z[index]=item) get(index) returns the item at index (similar to z[index] ) size() returns the number of items in the list (similar to z.length ) remove(index) removes the item at index (returns element that was removed) remove(value) removes first item that matches this value (returns true if item was found, else false) clear() removes all items from the list Adding an item: Adding an item To add an item to the end of an ArrayList , you can use the add() method. ArrayList names = new ArrayList (); names.add (“Abigail”); names.add (“Brian”); names.add (“Zach”); names.add (“Mallory”); 0 Abigail 1 Brian 2 Zach 3 Mallory Adding an item: Adding an item To add an item to the end of an ArrayList , you can use the add() method. Names.add (2, “Kelsey”); 0 Abigail 1 Brian 2 Zach 3 Mallory 0 Abigail 1 Brian 2 Kelsey 3 Zach 4 Mallory Adding an item: Adding an item To add an item to the end of an ArrayList , you can use the add() method. Names.set (3, “Marie”); 0 Abigail 1 Brian 2 Kelsey 3 Marie 4 Mallory 0 Abigail 1 Brian 2 Kelsey 3 Zach 4 Mallory What is the output.: What is the output. To add an item to the end of an ArrayList , you can use the add() method. ArrayList dogs= new ArrayList (); dogs.add (“Boxer”); dogs.add (“Basset Hound”); dogs.add (1, “Pomo”); dogs.add (0, “Mutt”); dogs.set (2, “German Shepard”); dogs.add (“Boxer”); dogs.remove (“Boxer”); What is the size of this ArrayList ? 0 Mutt 1 German Shepard 2 Basset Hound 3 Boxer Array Lists – Limitations : Array Lists – Limitations An ArrayList can be used to store any type of object reference. In fact, one ArrayList can store multiple types. HOWEVER ….. An ArrayList can NOT store primitive data types because those are not object references. When you want to store ArrayList elements, you must cast them to the appropriate reference before you store them OR you must declare a reference type in the ArrayList declaration. Casting an object : Casting an object For example, if you want to get a String out of the names ArrayList and save it, you have to cast the data as a String. ArrayList names = new ArrayList (); names.add (“Abigail”); names.add (“Brian”); String first Child ; firstLetter = (String) names.get (0); This coverts the generic returned object from the get() method to a String. If you do not perform this cast, you receive an error message of incompatible types. Wrapper Classes: Wrapper Classes Because numbers are NOT OBJECTS, you cannot directly insert them into ArrayLists. Instead, you must use wrapper classes. The classes Integer, Double, and Boolean wrap numbers and truth values inside objects. Wrapper Classes: Wrapper Classes The Double class is a typical number wrapper. There is a contructor that makes a Double object out of a double value: Conversely, the doubleValue method retrieves the double value that is stored inside a Double object. Double d = new Double(29.95); double x = d.doubleValue (); Using the Wrapper Class: Using the Wrapper Class Adding a primitive data type to an ArrayList : ArrayList data = new ArrayList ( ); double x = 29.95; Double wrapper = new Double(x); data.add (wrapper); After you have the number you want to add, change it into an object using the Wrapper class. Then add the number object to the ArrayList . Using the Wrapper Class: Using the Wrapper Class To retrieve the number, you need to cast the return value of the get method to Double and then call the doubleValue method: As you can see, it is a hassle to use the wrapper classes in order to store numbers into an ArrayList . ArrayList data = new ArrayList ( ); double x = 29.95; Double wrapper = new Double(x); data.add (wrapper); Double wrapper = (Double) data.get (0); double y = wrapper.doubleValue ( ); Cast it as a Double object then store it in an object reference Then call the doubleValue method and store as a primitive data type. Slide19: Lets check out some real Java rap! https://www.youtube.com/watch?v=b-Cr0EWwaTk Auto-Boxing: Auto-Boxing Starting with Java 5.0, conversion between primitive types and the corresponding wrapper classes is automatic . This process is called auto-boxing . If you assign a number to a Double object, the number is automatically "put into a box", namely a wrapper object. automatic Double d = 29.95; // auto-boxing; // same as Double d = new Double(29.95); Auto-Boxing: Auto-Boxing Conversely, wrapper objects are automatically "unboxed" to primitive types. Final thought: storing wrapped numbers is quite inefficient. The use of wrappers is acceptable for short array lists, but you should use arrays for long sequences of numbers or characters. double x = d; // auto-unboxing; // same as double x = d.doubleValue ( ); Practice: Practice Rewrite this code to include autoboxing. ArrayList aList = new ArrayList ( ); int x = 12; Integer wrapper = new Integer(x); aList.add (wrapper); Integer wrapper = (Integer) aList.get (0); int x = wrapper.integerValue ( ); ArrayList aList = new ArrayList ( ); Integer x = 12; aList.add (wrapper); int y = (Integer) aList.get (0); Arraylist with Specified type: Arraylist with Specified type You can eliminate the need to perform a cast with ArrayList objects by specifying the type that an ArrayList can hold. ArrayList <String> names = new ArrayList <String>(); names.add (“Abigail”); names.add (“Brian”); String first Child ; firstLetter = names.get (0); Arraylist with Specified type: Arraylist with Specified type You no longer have to use the cast operator Java checks to make sure that only items of appropriate types are added to the list The complier warning that indicates your program uses an unchecked or unsafe operation is eliminated. ArrayList <String> names = new ArrayList <String>(); Two Truths and a lie: Two Truths and a lie The Integer and Double classes are wrapper classes. You must cast object as you get them from an ArrayList unless you have specified a type. It is easy to use ints and double with ArrayLists.

Add a comment

Related presentations

Related pages

DCS128: ArrayLists notes - Welcome to EECS | School of ...

ArrayLists in Java, and introducing Generics ArrayLists as Arrays You have seen arrays in Java, where for any type, for example Thing, adding [] to the end ...
Read more

ArrayList class - Programming Tutorials and Lecture Notes

The ArrayList class builds upon the capabilities of arrays. An ArrayList object contains an array of object references plus many methods for managing that ...
Read more

java - ArrayList and BufferedReader Note Taking Program ...

I have created a simple program that takes a title and a note which you enter then you have a choice to export the notes to txt file using BufferedWriter ...
Read more

www.minich.com

ArrayLists. Objective #1: Be familiar with ArrayLists and use their available methods. An ArrayList is an object that can be used to store multiple objects ...
Read more

ArrayList (Java Platform SE 7 ) - Oracle Help Center

Each ArrayList instance has a capacity. ... Note that this implementation is not synchronized. If multiple threads access an ArrayList instance ...
Read more

APCS 4.7 - ArrayList notes by Jason Eiben on Prezi

APCS 4.7 - ArrayList notes. Unit 4 - Arrays, ArrayLists, and Classes by Jason Eiben on 15 December 2015 Tweet. Comments (0) Please log in to add your ...
Read more

DCS128: Sorting Arrays and ArrayLists notes: part 1

Sorting Arrays and ArrayLists: Part 1 ... since an arrayList can change in ... Note that while testing code by running it with data and observing the ...
Read more

Java: ArrayList - Fred Swartz - Home

Java Notes: ArrayList java.util.ArrayList allows for expandable arrays, and is basically the same as the older the Collections Vector class.
Read more

Java Notes: ArrayList (non-generic) - Lyle School of ...

Java Notes: ArrayList (non-generic) java.util.ArrayList allows for expandable arrays, and is the Collections replacement for the older Vector class. An
Read more