Differences and relationships between Array and ArrayList: arrayarraylist

Source: Internet
Author: User

Differences and relationships between Array and ArrayList: arrayarraylist

The blogger went to a java internship interview today and found that many of the most basic data structures in java are unfamiliar to the blogger. The interviewer asked some common data structures such as HashMap, the blogger can answer the question first. However, when asked about the differences and connections between Array and ArrayList, the blogger looked at the question. Okay, let's not talk about it. Now we will sort it out.

First, Array is an Array in java. We declare that there are three methods for java Arrays:

1 int [] a = new int [10]; 2 int a [] = new int [10]; // This method is the same as the C language 3 int a [] = {1, 2, 3, 4 };

From the preceding Declaration, we can see that when defining an array, we must specify the Data Type of this array, that is, the array is a set of the same data type. In addition, when declaring an array, we also declare the size of the array. The number of elements in the array is fixed.

Next, let's look at the Array application:

1 import java. util. arrays; 2 3/** 4 * @ author jy 5 * @ time 7:59:26 6 */7 public class ArrayAndArrayList {8 public static void main (String [] args) {9 10 int a [] = new int [4]; 11 System. out. println (. 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. It can be seen that the array does not overwrite the hashcode () and equals () Methods 16 System. out. println (Arrays. equals (B, c); // use java. util. arra Y equals () to determine whether the array is equal. Here, true17 System is output. out. println (isEquals (B, c )); 18 19} 20 21/** 22 * override method to manually compare arrays 23 */24 public static boolean isEquals (int [] B, int [] c) {25 26 if (B = null | c = null) {27 return false; 28} 29 if (B. length! = C. length) {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 unchangeable. The array does not overwrite the hashcCode () and equals () Methods of the object.

We all know that arrays can also be two-dimensional. Let's take a look at how two-dimensional arrays are declared:

1 int [] [] da = new int [2] [3]; // This declaration method is recommended, better indicate the type of the 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 cannot be blank. The size of the second dimension can be different. 2 dc [0] = new int [2]; 3 dc [1] = new int [3];

Well, here we will talk about the basic data structure application of arrays. To highlight the topic, we will not talk about other irrelevant applications.

 

Next, let's take a look at the ArrayList set:

ArrayList is a dynamic array, which is a complex version of the array. It can dynamically add and delete elements. 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, this list can be added to different types of elements without using generics, And the arraylist can be of no length specified. When using generics, we can only add one type of data.

The following code describes the important methods and attributes of ArrayList:

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 array 7 System. out. println (p [0]); 8 System. out. println (list. contains (4); // whether to include an element 9 System. out. println (list. size (); // The list length is 10 System. out. println (list. get (0); // get the elements in the list by bit
11 list. trimToSize (); // This method is used to fix the ArrayList to the actual element size. When the dynamic array element is determined not to be added, you can call this method to release the free memory.

The preceding shows some important ArrayList methods. Next we will compare the two collection classes:

(1) ArrayList is a complex version of Array.
ArrayList encapsulates an array of the Object type. In general, it has no essential difference with the array, and even many methods of ArrayList, for example, Index, IndexOf, Contains, and Sort all directly call the corresponding method of Array Based on the internal Array.

(2) Data Types stored

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

(3) Variable Length

The length of the Array is actually immutable, and the length of the two-dimensional variable-length Array is also fixed. The variable is only the length of the element. The length of the ArrayList can be specified (even if the length is specified, it will be automatically scaled up twice) or not.

(4) access and add/delete Elements

For general reference types, this part does not have a great impact, but for value types, adding and modifying elements to ArrayList will cause packing and unpacking operations, frequent operations may affect some efficiency. In addition, ArrayList is a dynamic array that does not include algorithms that use keys or values for quick access. Therefore, calling IndexOf, Contains, and other methods is actually a simple loop of execution to find elements, therefore, frequent calls to such methods are not faster than writing loops by yourself and are slightly optimized. If you have such requirements, we recommend that you use a set of key-value pairs such as Hashtable or SortedList.

 

Now, let's take a look at the comparison between the two data structures. If you find anything inappropriate, please let us know.

In addition, this is the first time I wrote a blog.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.