Using an ArrayList Flashcards
Give the signature of the most important methods used in ArrayList !
public boolean add(E e)
public void add(int index, E element)
ArrayList<Integer> list1=new ArrayList<Integer>();
list1.add(20);
list1.add(19);
list1.add(18);
System.out.println(list1.toString()); /* [20, 19, 18] */</Integer></Integer>
list1.add(1,127);
System.out.println(list1.toString()); // [20, 127, 19, 18]
public E remove(int index) :
ArrayList<Integer> list1=new ArrayList<Integer>();
list1.add(20);
list1.add(19);
list1.add(18);
System.out.println(list1.toString()); // [20, 19, 18]
Object o=list1.remove(1);
System.out.println(list1.toString()); // [20, 18]
System.out.println(o); // Prints 10</Integer></Integer>
public boolean remove(Object o) :
ArrayList<Integer> list1=new ArrayList<Integer>();
list1.add(20);
list1.add(19);
list1.add(18);
System.out.println(list1.toString()); // [20, 19, 18]
Object o=list1.remove((Integer)20);
System.out.println(list1.toString()); // [19, 18]
System.out.println(o); // true</Integer></Integer>
public E set(int index, E element) :
ArrayList<Integer> list1=new ArrayList<Integer>();
list1.add(20);
list1.add(19);
list1.add(18);
System.out.println(list1.toString()); // [20, 19, 18]
Object o=list1.set(1,127);
System.out.println(list1); // [20, 127, 18]
System.out.println(o); // 19</Integer></Integer>
public boolean isEmpty() :
ArrayList<Integer> list1=new ArrayList<Integer>();
list1.add(20);
list1.add(19);
list1.add(18);
System.out.println(list1.isEmpty()); //</Integer></Integer>
public int size() :
ArrayList<Integer> list1=new ArrayList<Integer>();
list1.add(20);
list1.add(19);
list1.add(18);
System.out.println(list1.size()); // Prints 3</Integer></Integer>
public void clear() :
ArrayList<Integer> list1=new ArrayList<Integer>();
list1.add(20);
list1.add(19);
list1.add(18);
list1.clear();
System.out.println(list1); // []</Integer></Integer>
public boolean contains(Object o) :
ArrayList<Integer> list1=new ArrayList<Integer>();
list1.add(20);
list1.add(19);
list1.add(18);
Object o=list1.contains(19);
System.out.println(list1); // [20,19,18]
System.out.println(o); // true</Integer></Integer>
public boolean equals(Object o) :
ArrayList<Integer> list1=new ArrayList<Integer>();
ArrayList<Integer> list2=new ArrayList<Integer>();
list1.add(20);
list1.add(19);
list1.add(18);
Object o=list1.equals(list2);
System.out.println(list1); // [20, 19, 18]
System.out.println(o); // false</Integer></Integer></Integer></Integer>
Give a characteristic of the add method !
Appending to the End: When you use the add(E e) method, the specified element is added to the end of the ArrayList. This operation increases the size of the list by one.
Order Preservation: The order of elements is preserved. If you add elements in a particular sequence, they will be maintained in that sequence in the ArrayList.
Dynamic Resizing: The ArrayList automatically resizes itself to accommodate the new element. This means you don’t need to worry about the internal capacity of the list; it will grow as needed.
Give a characteristic of the remove method !
A key characteristic of the remove method in the ArrayList class is that it can remove elements based on either their index or their value.
Removes the first occurrence of the specified element from the list if it exists. If the element is not present, the list remains unchanged.
Returns: true if the element was removed; false otherwise.
What is a slot ?
ArrayList list1 = new ArrayList(10);
Initial Capacity: The ArrayList is initialized with a capacity of 10. This means that the underlying array used by the ArrayList has space allocated for 10 elements initially.
Internal Storage: The ArrayList internally uses an array to store its elements. When you specify an initial capacity, you are effectively specifying the size of this internal array, although it is not immediately populated with elements.
Slots or Spaces: While the term “slots” is not used explicitly in the Java documentation, you could think of it as the number of positions or spaces in the internal array that are available to store elements. With an initial capacity of 10, the internal array has 10 “slots” or positions that can be filled with elements.
Dynamic Resizing: If the number of elements exceeds the initial capacity, the ArrayList will automatically resize its internal array to accommodate more elements. This resizing typically involves creating a new, larger array and copying the elements from the old array to the new one.
Give a characteristic of the equals method !
ArrayList <String> list1=new ArrayList<>();
ArrayList <String> list2=new ArrayList<>();
list1.add("A");
list1.add("B");
list1.add("C");</String></String>
list2.add(“A”);
list2.add(“B”);
list2.add(“C”);
System.out.println(list1.equals(list2)); /* PRINTS true */