ArrayList FAQ

1. How is the size of ArrayList automatically increased? Can you share your code?

This is one of the most technical questions, and most people can't answer it. In fact, when someone tries to add an object to the arraylist, Java checks the arraylist to make sure that the existing array has enough capacity to store the new object. If there is not enough capacity, a new array with a longer length will be created, the old array will be copied to the new array using the Arrays.copyOf method, and the existing array reference points to the new array. Take a look at the following code snippet (from the Java ArrayList Code at GrepCode.com):

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/ArrayList.java

//ArrayList Add方法:

public boolean add(E e){

    ensureCapacity(size+1); //Increment modCount!!

    elementData[size++] = e;

    return true;

}



//ensureCapacity方法:处理ArrayList的大小

public void ensureCapacity(int minCapacity) {

    modCount++;

    int oldCapacity = elementData.length;

    if (minCapacity > oldCapacity) {

    Object oldData[] = elementData;

    int newCapacity = (oldCapacity * 3)/2 + 1;

    if (newCapacity < minCapacity)

        newCapacity = minCapacity;

    // minCapacity is usually close to size, so this is a win:

    elementData = Arrays.copyOf(elementData, newCapacity);

    }

}

Note the situation where an array is created; the objects of the old array are copied into the new array, and the existing array points to the new array.

2. Under what circumstances would you use ArrayList? When would you choose LinkedList?

This is yet another question that most interviewers confuse. In most cases, you should use an ArrayList when you encounter elements that are accessed more frequently than elements are inserted or deleted. On the other hand, you would choose LinkedList when you are inserting or removing elements more frequently at a particular index, or when you don't need to access elements at all. The main reason here is that the worst time complexity for accessing an element in an ArrayList is "1", while in a LinkedList it might be "n". To add or delete an element in an ArrayList, the System.arraycopy method is usually called, which is an extremely resource-consuming operation. Therefore, in the case of frequent insertion or deletion of elements, the performance of LinkedList will be better.

3. When passing an ArrayList to a method, or a method returning an ArrayList, when should we consider the security risks? How to fix this security violation?

When an array is passed as a parameter to a method, this can happen if the array is directly assigned to a member variable without being copied, i.e. when the original array is changed by the calling method, The array passed into this method will also change. The code below shows the security violation and how to fix it.

ArrayList is directly assigned to member variables - security implications:

Fix this security risk:

4. How to copy an ArrayList to another ArrayList? write your code?

Here are a few techniques for copying an ArrayList into another ArrayList:

  1. Use the clone() method, such as ArrayList newArray = oldArray.clone();

  2. Use the ArrayList constructor, for example: ArrayList myObject = new ArrayList(myTempObject);

  3. Use Collection's copy method.

Note that 1 and 2 are shallow copies.

5. The operation process of adding or deleting an object in the ArrayList in the index? Is it inefficient? Explain why?

To add or delete elements in an ArrayList, you need to call System.arraycopy, which is an inefficient operation. If you encounter frequent insertion or deletion, you can choose other Java collections, such as LinkedList. Take a look at the code below:

Add an element at some index i of the ArrayList:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324891625&siteId=291194637