Array Lists Similarity between Array List and Array
Array. Lists
Similarity between Array. List and Array • An Array. List is similar to an array of Objects Differences between Array. List and Arrays • Arrays are natural data structures within Java. • Array. List is an added class. • An array is a fixed size • Array. List can grow and shrink • Arrays can hold primitive data or objects. • Array. List can only hold objects 2
Creating a Array. List The Object type must be specified using <> when creating an Array. List. Examples: Array. List<String> str. List 1 = new Array. List<String>(); Array. List<String> str. List 2 = new Array. List<String>(10); Array. List<Integer> age. List = new Array. List<Integer>(2); * Note the Integer wrapper class was used in place of a primitive data type. 3
Adding elements to an Array. List Elements can be added to the front, back, or elsewhere. 4
Adding elements to an Array. List void add(Object element) Examples: Adds the element to the back of the. Array. List <Integer> my. List = new Array. List<Integer>(); my. List. add(5); my. List. add(7); my. List. add(8); Result: [5, 7, 8] 5
Inserting elements within an Array. List void add(int index, Object element) • Inserts the element at position index in this Array. List • The index must be greater than or equal to zero and less than or equal to the number of elements in the Array. List <Integer> my. List = new Array. List<Integer>(); my. List. add(5); my. List. add(7); my. List. add(8); my. List. add(6, 1); Result: [5, 6, 7, 8] 6
Removing elements from an Array. List boolean remove(Object obj) • Removes the first occurrence of obj from this Array. List • 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 Array. List void clear() • Removes all elements 7
Accessing elements in an Array. List Object get(int index) • Returns the element at position index in this Array. List <Integer> my. List = new Array. List<Integer>(); my. List. add(5); my. List. add(7); my. List. add(8); System. out. println(my. List. get(1)); Output: 7 8
Modifying an element in an Array. List void set(int index, Object element) • Inserts the element at position index in this Array. List • The index must be greater than or equal to zero and less than or equal to the number of elements in the Array. List <Integer> my. List = new Array. List<Integer>(); my. List. add(5); my. List. add(8); my. List. set(1, 20); Result: [5, 20] 9
Comparing two Array. List 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. 10
Additional Functionality of an Array. List • boolean is. Empty() • Returns true if this Array. List has no elements • int size() • Returns the number of elements currently in this Array. List • Object[ ] to. Array() • Returns an array containing all the elements of this Array. List in the correct order 11
Searching a Array. List – Research on your own • boolean contains(Object element) • Tests if element is a component of this Array. List • Uses equals to test if it has found the correct element • int index. Of(Object element) • Returns the index of the first occurrence of element in this Array. List • Uses equals to test if it has found the correct element • Returns -1 if element was not found in this Array. List • int last. Index. Of(Object element) • Returns the index of the last occurrence of element in this Array. List • Uses equals to test if it has found the correct element • Returns -1 if element was not found in this Array. List 12
List of Common Array. List 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 index. Of(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 to. String() returns a string representation of the list such as "[3, 42, -7, 15]"
Additional Array. List methods: add. All(list) adds all elements from the given list to this list add. All(index, 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 contains. All(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() list. Iterator() returns an object used to examine the contents of the list (seen later) last. Index. Of(value) returns last index value is found in list (-1 if not found) remove(value) finds and removes the given value from this list remove. All(list) removes any elements found in the given list from this list retain. All(list) removes any elements not found in given list from this list sub. List(from, to) returns the sub-portion of the list between indexes from (inclusive) and to (exclusive) to. Array() returns the elements in this list as an array
Conclusion • A Array. List is like an array of Objects • The advantage of a Array. List is that you don’t need to know beforehand how big to make it • The disadvantage of a Array. List 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 Array. List instead 15
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. 16
Partial Solution Array. List<String> all. Words = new Array. List<String>(); Scanner in = new Scanner(System. in); String paragraph = in. next. Line(); Scanner input = new Scanner(paragraph); while (input. has. Next()) { String word = input. next(); all. Words. add(word); } System. out. println(all. Words); // remove all plural words for (int i = 0; i < all. Words. size(); i++) { String word = all. Words. get(i); if (word. ends. With("s")) { all. Words. remove(i); i--; } }
- Slides: 17