Presentation is loading. Please wait.

Presentation is loading. Please wait.

5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special.

Similar presentations


Presentation on theme: "5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special."— Presentation transcript:

1 5-May-15 ArrayLists

2 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special convenience syntax; ArrayList s don’t An array is a fixed size, but a ArrayList expands as you add things to it This means you don’t need to know the size beforehand Arrays can hold primitives or objects, but ArrayList s can only hold objects However, autoboxing can make it appear that an ArrayList can hold primitives

3 3 Generics In Java versions 1.4 and earlier, an ArrayList just held Object s, which could be of any (non-primitive) type For compatibility reasons you can still do this, but you will get a lot of warning messages In Java 5 you should specify the class of objects that the ArrayList will hold This is done with the new generics syntax

4 4 Creating a ArrayList the old way The syntax for creating ArrayList s has changed between Java 1.4 and Java 5 For compatibility reasons, the old way still works, but will give you warning messages Here are the (old) constructors: import java.util.ArrayList; ArrayList vec1 = new ArrayList(); Constructs an ArrayList with an initial capacity of 10 ArrayList vec2 = new ArrayList( initialCapacity );

5 5 Using an ArrayList the old way boolean add(Object obj ) Appends the object obj to the end of this ArrayList Example: myArrayList.add("A string is an object"); Object get(int index ) Returns the component at position index Note that this is returned as an Object ; to use it as a String, you must cast it Example: String s = (String)myArrayList.get(5);

6 6 Creating a ArrayList the new way Specify, in angle brackets after the name, the type of object that the class will hold Examples: ArrayList vec1 = new ArrayList (); ArrayList vec2 = new ArrayList (10); To get the old behavior, but without the warning messages, use the wildcard Example: ArrayList vec1 = new ArrayList ();

7 7 Adding elements to a ArrayList boolean add(Object obj ) Appends the object obj to the end of this ArrayList With generics, the obj must be of the correct type, or you get a compile-time (syntax) error Always returns true This is for consistency with other, similar classes void add(int index, Object element ) Inserts the element at position index in this ArrayList The index must be greater than or equal to zero and less than or equal to the number of elements in the ArrayList With generics, the obj must be of the correct type, or you get a compile-time (syntax) error

8 8 Removing elements boolean remove(Object obj ) Removes the first occurrence of obj from this ArrayList Returns true if an element was removed Uses equals to test if it has found the correct element void remove(int index ) Removes the element at position index from this ArrayList void clear() Removes all elements

9 9 Accessing with and without generics Object get(int index ) Returns the component at position index Using get the old way: ArrayList myList = new ArrayList(); myList.add("Some string"); String s = (String)myList.get(0); Using get the new way: ArrayList myList = new ArrayList (); myList.add("Some string"); String s = myList.get(0); Notice that casting is no longer necessary when we retrieve an element from a “genericized” ArrayList

10 10 Searching a ArrayList boolean contains(Object element ) Tests if element is a component of this ArrayList Uses equals to test if it has found the correct element int indexOf(Object element ) Returns the index of the first occurrence of element in this ArrayList Uses equals to test if it has found the correct element Returns -1 if element was not found in this ArrayList int lastIndexOf(Object element ) Returns the index of the last occurrence of element in this ArrayList Uses equals to test if it has found the correct element Returns -1 if element was not found in this ArrayList

11 11 Getting information boolean isEmpty() Returns true if this ArrayList has no elements int size() Returns the number of elements currently in this ArrayList Object[ ] toArray() Returns an array containing all the elements of this ArrayList in the correct order

12 12 More about equals There are many different notions of equality Example: two sets are equal if they contain the same elements; order of elements is irrelevant Java defines public boolean equals(Object) in the Object class, but equals is defined to be the same as == It’s often a good idea to override equals for your own objects If you do this, note that the argument should be a general Object The String class (and some others) override equals

13 13 Conclusion A ArrayList is like an array of Object s The advantage of a ArrayList is that you don’t need to know beforehand how big to make it The disadvantage of a ArrayList is that you can’t use the special syntax for arrays You should never use an array that you hope is “big enough”—use a ArrayList instead

14 14 The End “Where a calculator on the ENIAC is equipped with 18 000 vacuum tubes and weighs 30 tons, computers of the future may have only 1 000 vacuum tubes and perhaps weigh 1½ tons.” --Popular Mechanics, March 1949


Download ppt "5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special."

Similar presentations


Ads by Google