Creating an ArrayList Flashcards
What is the difference between an ArrayList and an Array ?
Arrays are a fixed-size data structure that stores elements of a specific type that is defined with a set length that cannot be changed after creation.
ArrayLists are a resizable array implementation of the List interface in the Java Collections Framework.
Arrays have a Fixed size which means that once an array is created, its length cannot be altered.
ArrayLists have a dynamic sizewhich means that the size of an ArrayList can change dynamically as elements are added or removed.
Arrays can store both primitives (e.g., int, char) and objects (e.g., String, custom classes). ArrayLists can only store objects; primitives must be wrapped in their corresponding wrapper classes (e.g., Integer for int, Double for double).
Give an important remark about code snippets in the exam that
don’t start with 1 !
Remember that if you are shown a code snippet with a line number that doesn’t begin with 1, you can assume the necessary imports are there.
Similarly, you can assume the imports are present if you are shown a snippet of a method.
Give all the ways we can declare and initialize an ArrayList
(especially after java 5) !
Default Initialization :
ArrayList<Type> list = new ArrayList<>();</Type>
Initialization with Initial Capacity :
ArrayList<Type> list = new ArrayList<>(initialCapacity);</Type>
Initialization with a Collection :
Collection<Type> collection = new ArrayList<>();
// Add elements to the collection
ArrayList<Type> list = new ArrayList<>(collection);</Type></Type>
Initialization with Arrays :
List<Type> list = new ArrayList<>(Arrays.asList(element1, element2, element3));</Type>
Give a characteristic of the symbol <and> when declaring
an ArrayList of a given type after JAVA 7 !</and>
Java 5 allows you to tell the compiler what the type would be by specifying it between <and>.</and>
Starting in Java 7, you can even omit that type from the right side.
This is called the diamond operator because <> looks like a diamond
ArrayList implements an interface called List, so you can store an ArrayList in a List reference variable but not vice
versa.
You can not omit in the right side of an ArrayList declaration
Can we store an arraylist in a list variable ?
We can store an ArrayList in a List variable, because ArrayList is class that implements the generic List interface