Topic 9.4: Working with Selected classes from the Java API - Declare and use an ArrayList of a given type Flashcards
Write a code snippet using the indexOf() method to find the index of a specific element in an ArrayList.
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");</String>
int index = names.indexOf(“Bob”);
This method returns true if the ArrayList has no elements, indicating that it is empty. Otherwise, it returns false.
What does the isEmpty() method return in an ArrayList?
What does the clear() method do in an ArrayList?
This method removes all elements from an ArrayList, resulting in an empty ArrayList.
What is the purpose of the clear() method in an ArrayList?
This method is used to remove all elements from an ArrayList, resulting in an empty ArrayList. It does not delete the ArrayList object itself but only removes its contents.
This method returns true if the specified element is found in the ArrayList, and false otherwise.
What does the contains() method return in an ArrayList?
This method is used to check whether an ArrayList contains a specific element. It returns a boolean value indicating whether the element is present in the ArrayList or not.
What is the purpose of the contains() method in an ArrayList?
Provide a code snippet that demonstrates how to replace an element in an ArrayList.
ArrayList<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Orange"); fruits.add("Banana"); fruits.set(1, "Mango");
How can you retrieve an element from an ArrayList?
To accomplish this , you can use the get(index) method.
ElementType element = list.get(index);
To accomplish this you can use the add() method.
ArrayList<ElementType> list = new ArrayList<>(); list.add(element);
How do you append elements to the end of an ArrayList?
To accomplish this , you can use the get(index) method.
ElementType element = list.get(index);
How can you retrieve an element from an ArrayList?
How can you insert elements at specific positions within an ArrayList?
To accomplish this, you can use the add(index, element) method
list.add(index, element);
What does the isEmpty() method return in an ArrayList?
This method returns true if the ArrayList has no elements, indicating that it is empty. Otherwise, it returns false.
You can accomplish this using the indexOf() method.
It returns the index of the first occurrence of the element if found, or -1 if the element is not present in the ArrayList.
How can you find the index of a specific element in an ArrayList
You can check this using the isEmpty() method.
It returns a boolean value indicating whether the ArrayList has any elements or not.
How can you check if an ArrayList is empty using the isEmpty() method?
What does the contains() method return in an ArrayList?
This method returns true if the specified element is found in the ArrayList, and false otherwise.
How can you remove elements from an ArrayList based on their object reference?
To accomplish this you can use the remove(element)
method.
list.remove(element);
Write a code snippet using the clear() method to remove all elements from an ArrayList.
ArrayList<String> names = new ArrayList<>(); names.add("Alice"); names.add("Bob"); names.clear();