Class ArrayList. Methods that define information about array elements
Contents
- 1. Method get(). Get element at given position
- 2. Method contains(). Determine if a list contains a specific element
- 3. Method containsAll(). Determine if all elements of some collection are in the given collection
- 4. Methods indexOf() and lastIndexOf(). Determine the position of the first and last occurrence of an element in an array
- 5. Methods iterator() and listIterator(). Get an iterator on the collection
- Related topics
Search other resources:
1. Method get(). Get element at given position
With the get() method, you can get the element at a given position. The method declaration is the following:
public E get(int index);
here
- E – type of array elements;
- index – the position of the element in the array starting from 0.0
Example.
import java.util.*; public class TrainCollections { public static void main(String[] args) { // Method get() - get element // 1. Create collection of numbers ArrayList<Integer> AL = new ArrayList(); AL.add(1); AL.add(2); AL.add(3); AL.add(4); AL.add(5); // 2. Get element at index 2 - get() method int item = AL.get(2); // 3. Display the element System.out.println("item = " + item); // item = 3 } }
Program result
item = 3
⇑
2. Method contains(). Determine if a list contains a specific element
The contains() method returns true if the list contains at least one specified element. According to the Java documentation, the general form of a method is as follows
public boolean contains(Object obj);
here
- obj is an element whose presence in the collection must be determined.
Example. The example detects the presence of the string “abcd” in an array of strings.
import java.util.*; public class TrainCollections { public static void main(String[] args) { // The contains() method - determine if an element is in an array // 1. Create a collection from strings ArrayList<String> AL = new ArrayList(); AL.add("abcd"); AL.add("abc"); AL.add("jklmn"); AL.add("jprst"); // 2. Invoke the contains() method boolean f_is = AL.contains("abcd"); // 3. Display the result System.out.println("f_is = " + f_is); } }
Program result
f_is = true
⇑
3. Method containsAll(). Determine if all elements of some collection are in the given collection
The containsAll() method can be used to determine whether all elements of one collection occur in another collection at least once. The general form of a method declaration is as follows
public boolean containsAll(Collection<?> obj);
here
- obj – collection whose elements are to be checked for presence in the current collection.
If all elements of the obj collection occur at least once in the current collection, the method returns true. If at least one element is not in the collection, the method returns false.
Example. The example creates two collections. Then there is a check for the presence of one collection in another.
import java.util.*; public class TrainCollections { public static void main(String[] args) { // The containsAll() method - determine the presence of the elements of the collection in the array // 1. Create a collection of numbers ArrayList<Integer> AL = new ArrayList(); AL.add(1); AL.add(2); AL.add(3); AL.add(4); AL.add(5); // 2. Create another collection of numbers ArrayList<Integer> AL2 = new ArrayList(); AL2.add(1); AL2.add(2); AL2.add(5); // 3. Invoke the containsAll() method boolean f_is_all = AL.containsAll(AL2); // 4. Display the result System.out.println("f_is_all = " + f_is_all); } }
Program result
f_is_all = true
⇑
4. Methods indexOf() and lastIndexOf(). Determine the position of the first and last occurrence of an element in an array
The indexOf() methods can be used to determine the position of the first or last occurrence of a given element in an array. The method declaration is as follows
public int indexOf(Object obj); public int lastIndexOf(Object obj);
here
- obj is the element whose position is to be determined.
If the obj element is not found in the array, then -1 is returned.
Example.
import java.util.*; public class TrainCollections { public static void main(String[] args) { // Methods indexOf(), lastIndexOf() // 1. Create a collection of 10 real numbers ArrayList<Double> AL = new ArrayList(); AL.add(1.5); AL.add(1.2); AL.add(1.2); AL.add(1.7); AL.add(1.7); AL.add(1.1); AL.add(1.2); AL.add(1.8); AL.add(1.6); AL.add(1.7); System.out.println(AL); // 2. Find the position of the first occurrence of the number 1.2 int pos = AL.indexOf(1.2); // pos = 1 System.out.println("indexOf(1.2) = " + pos); // 3. Find the position of the first occurrence of the number 1.7 pos = AL.indexOf(1.7); // pos = 3 System.out.println("indexOf(1.7) = " + pos); // 4. Find the position of the last occurrence of the number 1.2 pos = AL.lastIndexOf(1.2); System.out.println("lastIndexOf(1.2) = " + pos); // 5. Find the position of the last occurrence of the number 2.5 pos = AL.lastIndexOf(2.5); // pos = -1 - the number is not in the array System.out.println("lastIndexOf(2.5) = " + pos); } }
Program result
[1.5, 1.2, 1.2, 1.7, 1.7, 1.1, 1.2, 1.8, 1.6, 1.7] indexOf(1.2) = 1 indexOf(1.7) = 3 lastIndexOf(1.2) = 6 lastIndexOf(2.5) = -1
⇑
5. Methods iterator() and listIterator(). Get an iterator on the collection
The iterator() method allows you to get an iterator to the array. The listIterator() method returns an iterator to the array in the form of a list. The iterator can be used to traverse a collection. The general form of the method declaration form is:
public Iterator<E> iterator(); public ListIterator<E> listIterator();
here
- E – type of array elements.
Example. In our example, an array of characters is formed. Then, using the iterator and their hasNext() and next() methods, the array is traversed.
import java.util.*; public class TrainCollections { public static void main(String[] args) { // 1. Method iterator() - get the iterator to an array // 1.1. Создать коллекцию символов ArrayList<Character> AL1 = new ArrayList<Character>(); AL1.add('a'); AL1.add('b'); AL1.add('c'); AL1.add('d'); AL1.add('e'); // 1.2. Create iterator to the collection AL Iterator<Character> it1 = AL1.iterator(); // 1.3. Using a list iterator to iterate through a collection // and display the elements of a collection Character c; while (it1.hasNext()) { c = it1.next(); // get the element System.out.print(c + " "); } // 2. Method listIterator() // 2.1. Form a collection of numbers of type Short ArrayList<Short> AL2 = new ArrayList<Short>(); for (int i=0; i<5; i++) AL2.add((short) (i)); // AL2 = [ 0, 1, 2, 3, 4 ] // 2.2. Create list iterator on AL2 collection Iterator<Short> it2 = AL2.iterator(); // 2.3. Remove the first element from the collection (element 0) it2.next(); it2.remove(); // 2.4. Retrieve the rest of the collection System.out.println(); while (it2.hasNext()) { System.out.print(it2.next() + " "); } } }
Program result
a b c d e 1 2 3 4
⇑
Related topics
- Class ArrayList. Dynamic array. General information. Creating an array
- Methods that change data in the array. Methods add(), addAll(), clear(), remove(), removeAll(), removeIf(), replaceAll(), set(), sort()
- Methods that determine general characteristics of the array. Methods ensureCapacity(), isEmpty(), size(), trimToSize()
- Methods that transform the array as whole. Methods clone(), sublist(), toArray(), retainAll()
⇑