Arrays and Arraylists Flashcards

1
Q

What is the major difference between arrays and ArrayLists?

A

ArrayLists are dynamic and we do not have to specify the size while creating it. Arrays are created with a set immutable size.

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

What Java package is the ArrayList a part of?

A

java.util

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

What is the syntax to import an ArrayList?

A

import jave.util.ArrayList;

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

What is the syntax to create an array of double of the size 5 named score?

A

double[] score = new double[5];

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

What is the syntax to create an array of Strings named flavor with the data “Chocolate”, “Strawberry”, “Vanilla”?

A

String[] flavor = {“Chocolate”, “Strawberry”, “Vanilla”};

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

What is the syntax to create an ArrayList of data type Double of the size 5 named score?

A

ArrayList<Double> score = new ArrayList<>(5);
Note that the initial capacity is optional, but it can improve performance if you know the size of the ArrayList in advance.</Double>

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

What is the syntax to create an ArrayList of Strings named flavor with the data “Chocolate”, “Strawberry”, “Vanilla”?

A

ArrayList<String> flavor = new ArrayList<>();
flavor.add("Chocolate");
flavor.add("Strawberry");
flavor.add("Vanilla");</String>

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

What is the syntax to print out the values of a one dimensional array named flavor to the output console?

A

This will print the array like [xxx,xxx,xxx]
System.out.println(Arrays.toString(flavor));

These next two will print out each array item on it’s own line:
Arrays.stream(flavors).forEach(System.out::println);

for (String favor : flavors) {
System.out.println(flavor);
}

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

What is the syntax to print out the values of an ArrayList named flavor to the output console?

A

This will print the ArrayListlike [xxx,xxx,xxx]
System.out.println(Arrays.toString(flavor));

These next two will print out each ArrayList item on it’s own line:
for(String f : flavor) {
System.out.println(f);
}
Or
flavor.forEach(System.out::println);

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

What is the syntax to print out the values of a two dimensional array named flavor to the output console?

A

This will print out [[xxx,xxx],[xxx,xxx],[xxx,xxx]]
(notice the strings aren’t enclosed in “”)
System.out.println(Arrays.deepToString(flavor));

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

What is the syntax to remove the 3rd element in an ArrayList named flavor?

A

flavor.remove(3);

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

What is the syntax to add “Peppermint” to the end of an ArrayList named flavor?

A

flavor.add(“Peppermint”);

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

Can an ArrayList use primative data types?

A

No. We need to use the corresponding wrapper classes.

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

What is the class we need to import in order to declare a ArrayList with an array and what is the import syntax?

A

import java.util.Arrays;

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

What is the syntax to create an ArrayList of Strings named flavor with the data “Chocolate”, “Strawberry”, “Vanilla” using an ArrayList constructor? Include the import statements.

A

import java.util.ArrayList;
import java.util.Arrays;
ArrayList<String> flavor = new ArrayList<>(
Arrays.asList("Chocolate", "Strawberry", "Vanilla")
);</String>

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

What is the syntax to find the length of an array named footballScores and save the value into a int variable named numberOfScores?

A

numberOfScores = footballScores.length;

17
Q

What is the syntax to find the length of an array named footballScores and save the value into a int variable named numberOfScores?

A

numberOfScores = footballScores.length;

18
Q

What is the syntax to add “Cotton Candy” to a ArrayList named flavor?

A

flavor.add(“Cotton Candy”);

19
Q

What is the syntax to add “Cotton Candy” between “Chocolate” and “Strawberry” in a ArrayList named flavor with the data “Chocolate”, “Strawberry”, “Vanilla”?

A

flavor.add(1, “Cotton Candy”);

20
Q

What is the syntax for the two ways of removing “Cotton Candy” in a ArrayList named flavor with the data “Chocolate”, “Cotton Candy”, “Strawberry”, “Vanilla”?

A

flavor.remove(1);
When an int data type is passed into .remove the element at that index will be removed.
flavor.remove(“Cotton Candy”);
When a data type that is the same as the data type that is being stored by the ArrayList, is passed into .remove the first instance of that value found will be removed.

21
Q

When using .remove on an ArrayList with the data type int will java use the index or the value to find the element to be removed?

A

Since java will not know whether you mean the index or the value it will default to using the index.

22
Q

What is the syntax to print the item at index 2 in the array flavor with the data “Chocolate”, “Cotton Candy”, “Strawberry”, “Vanilla”? What will be printed out?

A

System.out.println(flavor[2]);
Strawberry

23
Q

What is the syntax to print the item at index 2 in the ArrayList flavor with the data “Chocolate”, “Cotton Candy”, “Strawberry”, “Vanilla”? What will be printed out?

A

System.out.println(flavor.get(2));
Strawberry

24
Q

What is the syntax to change “Strawberry” to “Blueberry” in the array flavor with the data “Chocolate”, “Cotton Candy”, “Strawberry”, “Vanilla” and print out the new result?

A

flavor[2] = “Blueberry”;
System.out.print(Arrays.toString(flavor));

25
Q

What is the syntax to change “Strawberry” to “Blueberry” in the ArrayList flavor with the data “Chocolate”, “Cotton Candy”, “Strawberry”, “Vanilla”

A

These will make the item change:
flavor.set(2, “Blueberry”);
or if we don’t know the index of the item we want to change:
int index = flavor.indexOf(“Strawberry”);
flavor.set(index, “Blueberry”);

26
Q

What happens when trying to print out an Array list with custom objects?

A

The output will be a list of memory addresses unless we use the special method named toString

27
Q

What is the difference between .length and .length()?

A

.length is for arrays
.length() is for strings
Neither can be used on the other.

28
Q

What does Arrays.asList do?

A

It allows us to insert a list into an ArrayList when creating it.
ArrayList<Strings> names = new ArrayList<>(Arrays.asList(“Tim”, “Steve”, “Sue”);</Strings>

29
Q

How do we find the index of the item “banana” in the ArrayList fruit?

A

fruit.indexOf(“banana”);

30
Q

What is the syntax to create a two dimensional array named fruitFlavors with these items:
“Apple”, “Banana”
“Cherry”, “Grape”
“Kiwi”, “Mango”
“Orange”, “Pineapple”

A

String[][] fruitFlavors = {
{“Apple”, “Banana”},
{“Cherry”, “Grape”},
{“Kiwi”, “Mango”},
{“Orange”, “Pineapple”}
};

31
Q

What is the syntax to access and print out “Banana” from this array:
fruitFlavors = {
{“Apple”, “Banana”},
{“Cherry”, “Grape”},
{“Kiwi”, “Mango”},
{“Orange”, “Pineapple”}
};

A

System.out.println(fruitFlavors[0][1]);

32
Q

what does .indexOf return if the item searched for isn’t in the array /ArrayList searched?

A

The indexOf() method returns -1 if the item is not found in the array/ArrayList.

33
Q

If you try to print an ArrayList of custom data type elements what will you get?

A

If you try to print an ArrayList of custom data type element, you will see a list of memory addresses.