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
An ArrayList is similar to an array of Objects Differences between ArrayList and Arrays Arrays are natural data structures within Java. ArrayList is an added class. An array is a fixed size ArrayList can grow and shrink Arrays can hold primitive data or objects. ArrayList can only hold objects

3 Creating a ArrayList The Object type must be specified using <> when creating an ArrayList. Examples: ArrayList<String> strList1 = new ArrayList<String>(); ArrayList<String> strList2 = new ArrayList<String>(10); ArrayList<Integer> ageList = new ArrayList<Integer>(2); * 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
void add(Object element) Examples: Adds the element to the back of theArrayList ArrayList <Integer> myList = new ArrayList<Integer>(); myList.add(5); myList.add(7); myList.add(8); Result: [5, 7, 8]

6 Inserting elements within an ArrayList
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 ArrayList <Integer> myList = new ArrayList<Integer>(); myList.add(5); myList.add(7); myList.add(8); myList.add(6, 1); Result: [5, 6, 7, 8]

7 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

8 Accessing elements in an ArrayList
Object get(int index) Returns the element at position index in this ArrayList ArrayList <Integer> myList = new ArrayList<Integer>(); myList.add(5); myList.add(7); myList.add(8); System.out.println(myList.get(1)); Output: 7

9 Modifying an element in an ArrayList
void set(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 ArrayList <Integer> myList = new ArrayList<Integer>(); myList.add(5); myList.add(8); myList.set(1, 20); Result: [5, 20]

10 Comparing two ArrayList
boolean equals( Object element) Determines if the two lists contain the same elements in the same order. equals() is defined to be the same as == * It is often a good idea to override equals for your own objects.

11 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

12 Searching a ArrayList – Research on your own
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

13 List of 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]"

14 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

15 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

16 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.

17 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