The difference between ArrayList and array

Array:
Advantages: continuous in memory, faster speed, simple operation.
 
Disadvantages: When you define an array, you need to define its length. It is not very flexible. Too long or too short will cause problems. It is not convenient to add, insert and remove data
 
 
When will the array be used?
 
1. Java ArrayList cannot store basic types, such as int and long. If it is saved, it needs to be encapsulated as Integer and Long. Automatic unpacking and unpacking will also cost performance. So to summarize this point, if you want to save basic types, Also pay special attention to performance, you can use arrays.
 
2. If the amount of data is known, the operation is very simple, and most of the methods in ArrayList are not needed, and the array can be used directly.
 
ArrayList:
Advantages: The size is dynamically expanded and contracted. There is no need to specify its length when declaring an ArrayList object. ArrayList inherits the List interface, which can easily add, insert and remove data.
 
Disadvantages: After inserting different types of data into the collection (ArrayList stores the data as objects), it is prone to type mismatch errors during data processing. Type conversion processing is required when using, and there are boxing and unboxing operations. A phenomenon that causes a lot of performance loss.
 
There are three initialization methods:
1. The default constructor will initialize the internal array with the default size
2. Use a Collection object to construct 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, the size when creating the need to define the initial size of the default value is a constant EMPTY ARRAY array of length 0, only when add (), will expansion to 10 , if the length of the array Not enough, the bottom layer will expand by 1.5 times the capacity, 10->15->22
 
ArrayList deduplication:
 
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();
        list.add("aaa");
        list.add("aaa");
        list.add("bbb");
        list.add("bbb");
        list.add("ccc");
        list.add("ccc");
        list.add("ccc");
        System.out.println(list);
        //双重循环去重
       System.out.println( removeDuplicate2(list));
        
    }
    
    //1.循环list中所有的元素然后删除
    public static ArrayList removeDuplicate1(ArrayList list){
        for(int i =0;i<list.size()-1;i++){
            for(int j=list.size()-1;j>i;j--){
                if(list.get(i).equals(list.get(j)))
                    list.remove(j);
            }
        }
        
        return list;        
    }
    
    //2.利用hashSet剔除重复元素,但是是无序的
    public static ArrayList removeDuplicate2(ArrayList list){
        HashSet set = new HashSet(list);
        //使用LinkedHashSet可以保证输入的顺序
        //LinkedHashSet<String> set2 = new LinkedHashSet<String>(list);
        list.clear();        
        list.addAll(set);
        return list;        
    }
    
    //3.利用list的contains方法去重
    public static ArrayList removeDuplicate3(ArrayList list){
        ArrayList tempList = new ArrayList(list.size());
        for(int i=0;i<list.size();i++){
            if(!tempList.contains(list.get(i)))
                tempList.add(list.get(i));
        }
        return tempList;        
    }
}

 

 

Guess you like

Origin blog.csdn.net/weixin_43562937/article/details/107214333