ArrayList in Java Flashcards

1
Q

what is an ArrayLists?

A

An ArrayLists is a resizable Array.

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

How do you create an ArrayLists?

A
  1. ArryList<Integer> integers;
    // null
    integers = new ArrayList<>();
    // the the object in the
    memory
    2.</Integer>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you add element to an array list?

A
  1. you can add element to an array list by using the Add(); method.
    a. fruits.add(“apple”);
    fruits.add(“Bananna);
    b.add(index , element)
    fruits.add(0 , apple);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you access element in an ArrayLists?

A
  1. you can access an element using the get(); method
    a.fruit.get(0);
    // get the first element in the ArrayList
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you change element in an ArrayList?

A
  1. we can change the element in the arrayList by using the set method.
    a. set(index, element)
    fruit.set(0, orange);
    // change the first element
    to orange in the array list.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you remove an element in an ArrayList?

A

To remove an element use the remove(); method
a.to remove by index:
fruit.remove(0);
// remove the first element in
the arraylist.

b. to remove by value:
fruit. remove(“apple”);
// remove the value apple from the arrayList

c. to remove all element use the clear(); method
fruit.clear();

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

How do we get the Size of an ArrayList?

A

To get the size of An ArrayList use the size(); method

a. fruit.size();
// get the size of the arraylist.

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

How to loop through an array list?

A

You can use a for loop to loop through an arraylist.
example:
for(int i = 0; i < fruit.size(); i++)
System.out.print(fruit.get(i));
// this loop and get the element in the array list using the size(); method

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

How do you sort An ArrayList?

A

To sort An array list you use the sort(); method from the collection class then pass the name of the arraylist to the sort(); method

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

what is the for each loop?

A

the for each loop is use for iterating over array and arraylists.

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

how to declare a for each loop

A

for( Type - varname : Arraylists/array)
example: itemArraylist.add(“orange);
for(String item : itemArrayList)
system.out.print(item + “ “

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

Why do we use array?

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