Differences between ArrayList and array

Advantages: it is continuous in memory, fast and easy to operate.
Disadvantages: when defining an array, its length should be defined. It is not very flexible. Too long or too short will cause problems. Inconvenient to add, insert and remove data
When will arrays be used?
1. java ArrayList cannot store basic types, int and long. If you save them, they need to be encapsulated as Integer and long. However, the automatic disassembly and assembly box also consumes performance. So to sum up, if you want to save basic types and pay special attention to performance, you can use arrays.
2. If the quantity and size of data are known, the operation is very simple, and most methods in ArrayList are not required. Arrays can also be used directly.
Advantages: size is dynamically expanded and contracted. You do not need to specify the length of an ArrayList object when declaring it. ArrayList inherits the List interface and can easily add, insert and remove data
Disadvantages: after inserting different types of data into the collection (ArrayList stores the data as an object), it is easy to make type mismatch errors during data processing. Type conversion is required during use. There are boxing and unpacking operations, resulting in a large loss of performance.
There are three initialization methods:
1. the default constructor will initialize the internal array with the default size
2. construct with a Collection object and add the elements of the Collection to the ArrayList
3. initialize the internal array with the specified size
The bottom layer is maintained by a variable array. There is no need to define the size when it is created. The default initial size is an array with a constant value of EMPTY ARRAY and a length of 0. It will be expanded to 10 only when add(). If the array length is insufficient, the bottom layer will expand its capacity according to 1.5 times the capacity. 10->15->22
ArrayList de duplication:
package com.xiaozhao.juc;

import java.util.ArrayList;
import java.util.HashSet;

public class ListDemo{
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        //Double cycle weight removal
       System.out.println( removeDuplicate2(list));
    //1. cycle through all elements in the list and delete them
    public static ArrayList removeDuplicate1(ArrayList list){
        for(int i =0;i<list.size()-1;i++){
            for(int j=list.size()-1;j>i;j--){
        return list;        
    //2. use hashSet to eliminate duplicate elements, but they are unordered
    public static ArrayList removeDuplicate2(ArrayList list){
        HashSet set = new HashSet(list);
        //Use LinkedHashSet to ensure the order of input
        //LinkedHashSet<String> set2 = new LinkedHashSet<String>(list);
        return list;        
    //3. use the contains method of the list to remove duplicates
    public static ArrayList removeDuplicate3(ArrayList list){
        ArrayList tempList = new ArrayList(list.size());
        for(int i=0;i<list.size();i++){
        return tempList;        



Tags: Interview

Posted by Pr0digy on Tue, 31 May 2022 18:09:15 +0530