Presentation is loading. Please wait.

Presentation is loading. Please wait.

ArrayLists.

Similar presentations


Presentation on theme: "ArrayLists."— Presentation transcript:

1 ArrayLists

2 Similarity between ArrayList and Array
A ArrayList is like an array of Objects Differences between ArrayList and Arrays Arrays have special convenience syntax; ArrayLists don’t An array is a fixed size, but an ArrayList can grow and shrink as you add and remove objects. This means you do not need to know the size beforehand Arrays can hold primitives or objects. ArrayLists can only hold objects

3 Creating a ArrayList The Object type must be specified when creating an ArrayList. Syntax: Specify, in angle brackets after the name, the type of object that the class will hold. Examples: ArrayList<String> strList1 = new ArrayList<String>(); ArrayList<String> strList2 = new ArrayList<String>(10); ArrayList<Card> deckOfCards = new ArrayList<Card>(); ArrayList<Integer> ageList = new ArrayList<Integer>(); * Note the Integer wrapper class was used in place of a primitive data type.

4 Adding elements to an ArrayList
Elements can be added to the front, back, or elsewhere.

5 Adding elements to an 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

6 Removing elements from an ArrayList
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

7 Accessing elements stored in an ArrayList
arrayListName.get(indexNumber) Returns the object at position index Example ArrayList<String> myFruit= new ArrayList<String>(); myList.add("GrapeFruit"); myList.add("Banana"); System.out.println(myFruit.get(1)); Output: Banana

8 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 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

9 Additional Functionality of an ArrayList
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

10 Most Common ArrayList methods:
add(value) appends value at end of list add(index, value) inserts given value just before the given index, shifting subsequent values to the right clear() removes all elements of the list indexOf(value) returns first index where given value is found in list (-1 if not found) get(index) returns the value at given index remove(index) removes/returns value at given index, shifting subsequent values to the left set(index, value) replaces value at given index with given value size() returns the number of elements in list toString() returns a string representation of the list such as "[3, 42, -7, 15]"

11 Additional ArrayList methods:
addAll(list) addAll(index, list) adds all elements from the given list to this list (at the end of the list, or inserts them at the given index) contains(value) returns true if given value is found somewhere in this list containsAll(list) returns true if this list contains every element from given list equals(list) returns true if given other list contains the same elements iterator() listIterator() returns an object used to examine the contents of the list (seen later) lastIndexOf(value) returns last index value is found in list (-1 if not found) remove(value) finds and removes the given value from this list removeAll(list) removes any elements found in the given list from this list retainAll(list) removes any elements not found in given list from this list subList(from, to) returns the sub-portion of the list between indexes from (inclusive) and to (exclusive) toArray() returns the elements in this list as an array

12 Considerations for the equals method
There are many different notions of equality Example 1: Two lists are equal if they contain the same elements; order of elements is irrelevant Example 2: Two lists are equal if they contain the same elements in the same order. 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 Conclusion A ArrayList is like an array of Objects
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 an ArrayList instead

14 Exercise Write a program that reads a paragraph and displays the individual words as a list. First display all words. Then display them in reverse order. Then display them with all plurals (ending in "s") capitalized. Then display them with all plural words removed.

15 Partial Solution ArrayList<String> allWords = new ArrayList<String>(); Scanner in = new Scanner(System.in); String paragraph = in.nextLine(); Scanner input = new Scanner(paragraph); while (input.hasNext()) { String word = input.next(); allWords.add(word); } System.out.println(allWords); // remove all plural words for (int i = 0; i < allWords.size(); i++) { String word = allWords.get(i); if (word.endsWith("s")) { allWords.remove(i); i--;


Download ppt "ArrayLists."

Similar presentations


Ads by Google