Difference between Array and ArrayList in java

1) Incisive explanation: ArrayList
can be  imagined as an "Array that will automatically expand its capacity".

2) Array([]): The most efficient; but its capacity is fixed and cannot be changed dynamically;
     ArrayList: The capacity can be dynamically increased; but efficiency is sacrificed;

3) Recommendation:
Based on efficiency and type checking, Array should be used as much as possible , and the array cannot be determined Only use ArrayList when size !
But when you try to solve more general problems, the capabilities of Array can be too limited.

4) Everything in Java is an object, and Array is also an object. No matter what type of Array you use,

The Array name itself is actually a reference, pointing to an actual object within the heap.

This object can be created automatically via the "Array initialization syntax", or it can be created manually with a new expression.

5) Array can be used as the return value of the function , because it is the reference of the object itself;

6) The application of the object array and the basic type array is almost exactly the same, the only difference is that the former holds the reference, and the latter directly holds the basic type Other values;
for example:
string [] staff=new string[100];
int [] num=new int[10];

7) What the container holds is actually a reference to Object, which can store any type. Of course this does not include base types, because base types do not inherit from any classes.

8) In the face of Array, we can directly hold the Array of basic type values ​​(for example: int [] num;), and also can hold the Array of reference (pointing to the object); but the container class can only hold the reference (pointing to the object) ), if you want to put the basic type in the container, you need to use the wrapper class. But the wrapper class may not be very easy to use, in addition, the efficiency of primitives Array is much better than the container that "holds the wrapper class (reference) over the basic type".

Of course, if your operation object is a basic type, and you need to automatically expand the capacity when the space is insufficient, Array is not suitable, and you have to use the container of the outer class.

9) In some cases, the container class can still work correctly even if it is not converted to its original type. There is one special case: the compiler provides some additional support for the String class, making it work smoothly.

10) Some basic operations on arrays like sorting, searching and comparing are common. Therefore, the Arrays class is provided in Java to assist these operations: sort(), binarySearch(), equals(), fill(), asList().

However, the Arrays class does not provide a delete method, and the ArrayList has a remove() method , I don't know if it's the reason why you don't need to delete and other operations in Array (because you should use a linked list at this time).

11) The use of ArrayList is also very simple: generate ArrayList, use add() to put objects in, and use get(i) to combine index values ​​to take them out. All of this is exactly the same as how Array is used, except that [] is missing.

2. References:
1) Efficiency:
Array expansion is a factor that greatly affects the efficiency of ArrayList.
Whenever the method of adding elements such as Add, AddRange, Insert, InsertRange is executed, it will check whether the capacity of the internal array is not enough . If so, it will rebuild an array with twice the current capacity, and copy the old elements to the new ones. In the array, and then discard the old array, the expansion operation at this critical point should be more efficient.

ArrayList is a complex version of Array.
ArrayList encapsulates an array of Object type. In a general sense, it is not fundamentally different from arrays. Even many methods of ArrayList, such as Index, IndexOf, Contains, Sort, etc. are all in the On the basis of the internal array, the corresponding method of Array is directly called.

2) Type identification:
When ArrayList stores an object, the type information is discarded, all objects are masked as Object, and the type is not checked at compile time, but an error will be reported at runtime.
The difference between ArrayList and array is mainly due to the efficiency of dynamic expansion

3) ArrayList can store any Object, such as String.

Reprinted from: http://blog.csdn.net/chenglansky/article/details/44567887

The mutual conversion of Array and ArrayList

public class Test {  
    public static void main(String[] args) {  
        List<String> list=new ArrayList<String>();  
        list.add("Wang Lihu");  
        list.add("Zhang San");  
        list.add("Li Si");  
        int size=list.size();  
        String[] array = (String[])list.toArray(new String[size]);  
        for(int i=0;i<array.length;i++){  
            System.out.println(array[i]);  
        }  
    }  
}
package test.test1;  
  
import java.util.Arrays;  
import java.util.List;  
  
public class Test {  
    public static void main(String[] args) {  
        String[] array=new String[3];  
        array[0]="Wang Lihu";  
        array[1]="Zhang San";  
        array[2]="Li Si";  
        List<String> list=Arrays.asList(array);  
        for(int i=0;i<list.size();i++){  
            System.out.println(list.get(i));  
        }            
    }  
}

Guess you like

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