Collections Flashcards

1
Q

ArrayList methods

A

add(2, elem5)–> adds the elem5 on the index 2 and pushes all the elements to index+1
listVarible.subList(from index, toIndex)–> where toIndex is exclusive!
*if fromIndex=toIndex then the subList methods returns an empty list

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

Benefits of ArrayList over an array

A

**You do not have to worry about the size of the ArrayList while appending elements.
An ArrayList resized dynamically at run time as per the situation. An array cannot be resized once created. This reduces the amount of boiler plate code that is required to do the same task using an array.

**it may consume more memory space:an ArrayList may consume a little bit more memory than an array (because of additional internal data structure and pointers), while in some other situation it may consume less (when your array is only half full).

**An ArrayList, just like an array is not thread safe!!!!!
**They allow you to write type safe code.
Since ArrayList is a generics enabled class, it helps you write type safe code. For example, if you have:
ArrayList<String> al = new ArrayList<>();
al.add(new Integer(10)); will not compile because the compiler knows that al can only contain Strings.</String>

However, this is not an advantage over an array because arrays are also type safe. For example, if you have:
String[] sa = new String[10];
you cannot do sa[0] = new Integer(10); either.

But you can do Object[] oa = sa; and oa[0] = new Integer(10); This will compile fine but will fail at runtime. This is a hole in the type safety provided by arrays.

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

ArrayList

A

ArayList is a subclass of AbstractList.
java.lang.Object
- java.util.AbstractCollection<E>
- java.util.AbstractList<E>
- java.util.ArrayList<E>
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>,
List<E>, RandomAccess
-->RandomAccess is a marker interface used by List implementations to indicate that they support fast (generally constant time) random access. The primary purpose of this interface is to allow generic algorithms to alter their behavior to provide good performance when applied to either random or sequential access lists.</E></E></E></E></E></E>

***It allows you to access its elements in random order –> because you can directly access any element using get(index) method. (This is unlike a LinkedList, in which you have to go through all the elements occuring before Nth element before you can access the Nth element.)

** you can still use non-generic form. For example, instead of using
ArrayList<String> listOfStrings;
you can use:
ArrayList listOfStrings;--> you will lose the compile time type checking</String>

**An ArrayList is a List so you can use it where ever a List is required. This include Collections methods such as sort, reverse, and shuffle.

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

Array List quarks

A

ArrayList<Double> al = new ArrayList<>();
--> the add method is typed to accept only a Double.!!!!
-->ArrayList does not have a field named length. It does have a method named size() though. So you can do:
Double d = al.get(al.size()); It will compile but will throw IndexOutOfBoundsException at run time in this case because al.size() will return 0 and al.get(0) will try to get the first element in the list.</Double>

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

Arrays of primitives

A

All the elements of an array of primitives are automatically initialized by default values, which is 0 for numeric types and false for boolean.

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