Differences and Relations between Array and Array List

Posted by luminous on Sat, 22 Jun 2019 21:08:00 +0200

The blogger went to an internship interview in java today and found that many of the most basic data structures in java are unfamiliar to the blogger. When the interviewer asked some common data structures such as HashMap, the blogger could answer the question first, but when asked about the difference and connection between Array and Array List, the blogger was confused. Okay, let's not say much. Now I'll sort it out.

First of all, Array is an array in java. We declare that there are three ways of arrays in java:

1 int[] a = new int[10]; 
2 int a[] = new int[10]; //This is the same way. c Language is the same
3 int a[] = {1,2,3,4}; 

As you can see from the statement above, when we define an array, we must specify the data type of the array, that is, the array is a collection of the same data type. In addition, when we declare an array, we also declare the size of the array, and the number of elements of the array is fixed.

Next, let's look at the application of arrays:

 1 import java.util.Arrays;
 2 
 3 /**
 4  * @author jy
 5  * @time 7:59:26 p.m.
 6  */
 7 public class ArrayAndArrayList {
 8     public static void main(String[] args) {
 9         
10      int a[] = new int[4];
11      System.out.println(a.length);  //Array Length Attribute
12      
13      int b[] = {1,2};
14      int c[] = {1,2};
15      System.out.println(b.equals(c));  //The output is false,Visible arrays are not rewritten hashcode()and equals()Method
16      System.out.println(Arrays.equals(b, c));  //utilize java.util.Array Of equals()To determine whether an array is equal,Here output true
17      System.out.println(isEquals(b,c));
18      
19     }
20 
21     /**
22      * Rewrite method to manually implement comparison between arrays
23      */
24     public static boolean isEquals(int[] b, int[] c) {
25         
26         if(b.length != c.length){
27             return false;
28         }
29         if(b == null ||c == null){
30             return false;
31         }
32         for (int i = 0; i < c.length; i ++) {
33             if(b[i] != c[i]){
34                 return false;
35             }
36         }
37         return true;
38     }
39     
40 }

It can be seen that the length of the array is fixed and immutable. The array does not override the object's hashcCode() and equals() methods.

As we all know, arrays can also be two-dimensional. Let's look at how two-dimensional arrays are declared.

1 int[][] da = new int[2][3];  //This declaration is recommended to better indicate the type of array.
2 int db[][] = new int[4][3];

However, there is a variable-length two-dimensional array:

1 int[][] dc = new int[2][];  //The size of the first dimension can not be vacant, and the size of the second dimension can be different.
2 dc[0] = new int[2];
3 dc[1] = new int[3];

Well, here's the basic data structure application of arrays. In order to highlight the topic, we'll leave aside some other irrelevant applications.

 

Next, let's look at the ArrayList collection:

ArrayList is a dynamic array, which is a complex version of the array. It can add and delete elements dynamically. ArrayList implements the java.util.Collections.Collection.List interface. Let's take a look at the most basic statement:

ArrayList list = new ArrayList(10);  
ArrayList<Integer> list1 = new ArrayList<Integer>();

In the first declaration, without generics, this list can be added to different types of elements, and arraylist can be added without specifying the length. When using generics, we can only add one type of data.

The important methods and attributes of ArrayList are shown in the following code:

 1 ArrayList<Integer> list = new ArrayList<Integer>();
 2         list.add(1);
 3         list.add(2);
 4         list.add(3);
 5         list.remove(1);
 6         Object[] p = list.toArray();  //Convert to an array
 7         System.out.println(p[0]);
 8         System.out.println(list.contains(4));  //Does it contain an element?
 9         System.out.println(list.size());  //list Length
10         System.out.println(list.get(0));  //Bit-by-bit acquisition list Elements in
11 list.trimToSize();  //This method is used to fix the ArrayList to the size of the actual element. When the dynamic array element is determined not to be added, this method can be called to release the free memory.

Some important methods of ArrayList are shown above. Let's compare these two collection classes:

(1) Array List is a complex version of Array
ArrayList encapsulates an array of Object type, which is not essentially different from arrays in general sense. Even many methods of ArrayList, such as Index, IndexOf, Contains, Sort, etc., call Array's corresponding methods directly on the basis of internal arrays.

(2) Data type stored

Array List can store heterogeneous objects, while Array can only store data of the same data type.

(3) Variable length

Array's length is actually immutable, and two-dimensional variable-length arrays are actually fixed in length, only the length of the elements in them can be changed. The length of the ArrayList can be specified (even if the length is specified, it will automatically double the capacity) or not, and it will become longer.

(4) Accessing and deleting elements

For general reference types, this part has little impact, but for value types, adding and modifying elements to ArrayList can cause boxing and unboxing operations, and frequent operations may affect some of the efficiency. In addition, ArrayList is a dynamic array, it does not include fast access algorithm through Key or Value, so in fact calling IndexOf, Contains and other methods are simple loops to find elements, so frequent invocation of such methods is not faster than your own write loop and slightly optimized. If you have this requirement, it is recommended to use Hashtable or SortedList and other key values. A set of pairs.

 

OK, we'll come to this point about the comparison of these two data structures. If there's something wrong with them, you're welcome to correct it.

In addition, this is my first time to write a blog, asking for light spray and embracing.

Topics: Java Attribute C