• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/20

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

20 Cards in this Set

  • Front
  • Back
What is the benefit of using ArrayList instead of an array?
ArrayList stores an expendable collection of objects, so you can add, delete and modify the array, without reinventing the wheel.
How does an ArrayList get created and what happens behind the scenes?
To create an ArrayList use keyword ArrayList with type of ArrayList in angular brackets and use keyword new on the right side of the assignment sign followed by the ArrayList contructor and optionally ArrayList type in angular brackets. On the backend an array gets created, but through methods it may be reduced and increased in size, which makes it dynamic.
Do you need to specify size of ArrayList to allocate memory?
No, if the constructor with no arguments is used, by default the array gets capacity of 10, or you can optionally specify initial size as an integer during creation of ArrayList.
How does add() method work for ArrayList?
If no particular position is specified, first it checks that capacity of the underlying array has an available spot at the end. If not, it uses ensureCapacity(x) method to create a new array with more capacity and places the value at the first spot where no value exists. Adding a particular position creates a new array and shifts values if necessary to allow the new value in.
What are the 2 common ways to access elements of an ArrayList?
1) Enhanced for loop, which is build using type of array, variable referring to each element, colon sign and ArrayList variable. 2) ListIterator, created by calling listIterator() method on the ArrayList, then hasNext() and next() methods are used to access the elements in a while loop. listIterator allows removing the accessed item, while for-loop doesn't.
How does ArrayList remove() method work?
Remove() method takes either the index of the position or an object as arguments. In case of an object, the method uses equality to identify what to remove, for example if a variable is passed, it checks what element of the array references the same object, or in case of a constructor, it identifies whether there's an element equal to the constructed argument. This method removes and returns the the first occurrence of specified element. The method returns true if the value was present and false if the value wasn't there.
How can you modify ArrayList elements?
Access the element using for-loop or listIterator, then modify it, or use set() method with element index passed as integer. Set is used to MODIFY elements, meaning there has to be an element in the spot. (you can't use index out of bounds of ArrayList size)
What happens when an object referenced by the ArrayList is modified?
If the object modified is referenced through the array or variable referencing the same object, the modifications are visible to the ArrayList. It becomes tricky when another ArrayList references part or all of the first ArrayList.
How can multiple elements be added to an ArrayList at once?
Using another ArrayList and addAll() method, with startposition integer optional.
What methods are used to delete all elements of an ArrayList?
clear() method with no arguments.
How do contains(), indexOf() and lastIndexOf() methods work in ArrayList?
They all use equals() method to identify equality between the passed object and any element of the ArrayList. Be careful with how equals() method is defined for each class, or you can override them to work differently. These methods take type object, so will almost always compile.
How does clone() method work for ArrayList?
Clone() returns a new object of type Java Object and needs to be cast to ArrayList of whatever type to be assigned to a reference variable. The object is different from the one referenced by the original ArrayList. However, the individual elements still reference the same objects, they were not copied.
What does the toArray() method do in ArrayList class?
It creates an array, copies the elements of the ArrayList to it and returns the new array. However, the references to individual element objects are preserved in the returned array, so modifications to them will be visible to both the new array and the original ArrayList.
How do you override equals() method in a class?
You have to keep the return type, parameter type and name of the method the same to override it from default Java Object method. Otherwise, it will not be used during comparison of equality in other methods, like contains() for example. You should also use the contract for the equals method. It is recommended to override hashCode() Method along with equals()
List the rules of contract for equals() Method
The equality relationship between non-null object references is 1) reflexive (equality to self), 2) symmetric (bidirectional equality), 3) transitive (shared between 3 or more objects), 4) consistent (on multiple invocations) 5) non empty, meaning the equality to null is false.
Does specifying large capacity of ArrayList initially improve efficiency with frequently expanded ArrayLists?
Yes, that way the ArrayList won't have to keep copying arrays when you add elements to it (at least for a while)
Can you use ArrayList objects without importing any packages into the class?
No, you have to either import all classes from java.util, or just java.util.ArrayList.
If ArrayList 1 has been expanded using addAll() from ArrayList 2, what would happen if one of the elements in ArrayList 2 is modified?
If the element is modified, but the object referenced stayed the same (for example in case of StringBuilder), ArrayList 1 will also see the modification. However, if the element was modified being replaced by a new object, then ArrayList 1 will not see that change.
Can you use remove() method on ArrayList that hasn't been initialized?
Yes, if you use the remove (object) method, and not remove with index of position. If you attempt to use remove with index on an ArrayList that hasn't been initiated or that is out of bounds, you'll get runtime exception - indexoutofbounds.
What happens to the ArrayList elements when you remove an element in the beginning of middle of the ArrayList?
The rest of elements shift leftwards to fill in the empty spot. Indexes shift along with that.