Topic 9.4: Working with Selected classes from the Java API - Declare and use an ArrayList of a given type Flashcards

1
Q

Write a code snippet using the indexOf() method to find the index of a specific element in an ArrayList.

A

ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");</String>

int index = names.indexOf(“Bob”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

This method returns true if the ArrayList has no elements, indicating that it is empty. Otherwise, it returns false.

A

What does the isEmpty() method return in an ArrayList?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the clear() method do in an ArrayList?

A

This method removes all elements from an ArrayList, resulting in an empty ArrayList.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the purpose of the clear() method in an ArrayList?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

This method returns true if the specified element is found in the ArrayList, and false otherwise.

A

What does the contains() method return in an ArrayList?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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.

A

What is the purpose of the contains() method in an ArrayList?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Provide a code snippet that demonstrates how to replace an element in an ArrayList.

A
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Banana");

fruits.set(1, "Mango");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you retrieve an element from an ArrayList?

A

To accomplish this , you can use the get(index) method.

ElementType element = list.get(index);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

To accomplish this you can use the add() method.

ArrayList<ElementType> list = new ArrayList<>();
list.add(element);
A

How do you append elements to the end of an ArrayList?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

To accomplish this , you can use the get(index) method.

ElementType element = list.get(index);

A

How can you retrieve an element from an ArrayList?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can you insert elements at specific positions within an ArrayList?

A

To accomplish this, you can use the add(index, element) method

list.add(index, element);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the isEmpty() method return in an ArrayList?

A

This method returns true if the ArrayList has no elements, indicating that it is empty. Otherwise, it returns false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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.

A

How can you find the index of a specific element in an ArrayList

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

You can check this using the isEmpty() method.

It returns a boolean value indicating whether the ArrayList has any elements or not.

A

How can you check if an ArrayList is empty using the isEmpty() method?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does the contains() method return in an ArrayList?

A

This method returns true if the specified element is found in the ArrayList, and false otherwise.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How can you remove elements from an ArrayList based on their object reference?

A

To accomplish this you can use the remove(element) method.

list.remove(element);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Write a code snippet using the clear() method to remove all elements from an ArrayList.

A
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");

names.clear();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Write a code snippet using the contains() method to check if an ArrayList contains a specific element.

A
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(5);
numbers.add(10);

boolean contains = numbers.contains(10);
19
Q

To accomplish this, you can use the add(index, element) method

list.add(index, element);

A

How can you insert elements at specific positions within an ArrayList?

20
Q

to accomplish this you can use the remove(index) method.

list.remove(index);

A

How can you remove elements from an ArrayList based on their index?

21
Q

How can you determine the number of elements in an ArrayList?

A

To achieve this, you can use the size() method.

int size = list.size();

22
Q

Write a code snippet using the isEmpty() method to check if an ArrayList is empty.

A
ArrayList<String> names = new ArrayList<>();
names.add("Alice");

boolean isEmpty = names.isEmpty();
23
Q

Provide a code snippet that demonstrates how to remove an element from an ArrayList based on its object reference.

A
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(5);
numbers.add(10);
numbers.add(15);

numbers.remove(Integer.valueOf(10));
24
Q

How can you find the index of a specific element in an ArrayList

A

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.

25
Q

This method returns the index of the first occurrence of the specified element in the ArrayList. If the element is not found, it returns -1.

A

What does the indexOf() method return in an ArrayList?

26
Q

Provide a code snippet that demonstrates how to determine the size of an ArrayList.

A
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Banana");

int size = fruits.size();
System.out.println(size);
27
Q

Provide a code snippet that demonstrates how to insert an element at a specific position within an ArrayList.

A
ArrayList<String> colors = new ArrayList<>();
colors.add("Red");
colors.add("Blue");
colors.add(1, "Green");
28
Q

Provide a code snippet that demonstrates how to append an element to the end of an ArrayList.

A
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
29
Q

What is the purpose of the ArrayList class in Java?

A

The purpose of the ArrayList class in Java is to provide a resizable and dynamic array implementation that serves as a container for storing a collection of objects.

It allows for flexible management of elements, allowing the ArrayList to grow or shrink in size as elements are added or removed.

30
Q

How can you replace an element in an ArrayList?

A

To modify an element in an ArrayList, you can use the set(index, element) method. This method replaces the element at the specified index with the specified element, updating the ArrayList with the new value while preserving its structure.

list.set(index, element);

31
Q

How do you append elements to the end of an ArrayList?

A

To accomplish this you can use the add() method.

ArrayList<ElementType> list = new ArrayList<>();
list.add(element);
32
Q

How can you remove elements from an ArrayList based on their index?

A

to accomplish this you can use the remove(index) method.

list.remove(index);

33
Q

Provide a code snippet that demonstrates how to remove an element from an ArrayList based on its index.

A
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Banana");

fruits.remove(1);
34
Q
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Banana");

fruits.remove(1);
A

Provide a code snippet that demonstrates how to remove an element from an ArrayList based on its index.

35
Q

This method removes all elements from an ArrayList, resulting in an empty ArrayList.

A

What does the clear() method do in an ArrayList?

36
Q

To achieve this, you can use the size() method.

int size = list.size();

A

How can you determine the number of elements in an ArrayList?

37
Q
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Banana");

int size = fruits.size();
System.out.println(size);
A

Provide a code snippet that demonstrates how to determine the size of an ArrayList.

38
Q

To accomplish this you can use the remove(element) method.

list.remove(element);

A

How can you remove elements from an ArrayList based on their object reference?

39
Q

What does the indexOf() method return in an ArrayList?

A

This method returns the index of the first occurrence of the specified element in the ArrayList. If the element is not found, it returns -1.

40
Q

How can you remove elements from an ArrayList, and what are the methods involved?

A

You can remove elements from an ArrayList using two methods: remove(index) and remove(element).

41
Q

How can you check if an ArrayList is empty using the isEmpty() method?

A

You can check this using the isEmpty() method.

It returns a boolean value indicating whether the ArrayList has any elements or not.

42
Q

What is the purpose of using an ArrayList over a regular array when dealing with the issue of running out of room?

A

The purpose of using an ArrayList over a regular array is to overcome the problem of running out of room. With an ArrayList, you don’t need to manually create a larger array and copy the contents. The ArrayList handles this internally, providing benefits such as not having to write code for resizing and ensuring that all references to the ArrayList remain valid.

43
Q

What is the purpose of the contains() method in an ArrayList?

A

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.

44
Q
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(5);
numbers.add(10);
numbers.add(15);

numbers.remove(Integer.valueOf(10));
A

Provide a code snippet that demonstrates how to remove an element from an ArrayList based on its object reference.