Home  >  Article  >  Java  >  The difference and connection between Array and ArrayList

The difference and connection between Array and ArrayList

巴扎黑
巴扎黑Original
2017-06-26 10:22:592270browse

The blogger went to a Java internship interview today and found that many of the most basic data structures in Java were unfamiliar to the blogger. When the interviewer asked about some common data structures such as HashMap, the blogger could answer The explanation was clear and logical, but after asking about the difference and connection between Array and ArrayList, the blogger was confused. Okay, not much to say, just sort it out now.

First of all, Array is an array in java. There are three ways to declare an array in java:

1 int[] a = new int[10]; 2 int a[] = new int[10]; //这种方式同c语言一样3 int a[] = {1,2,3,4};

As can be seen from the above declaration, we When defining an array, the data type of the array must be specified, that is, the array is a collection of the same data type. In addition, when declaring the 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 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);  //数组的长度属性12      13      int b[] = {1,2};14      int c[] = {1,2};15      System.out.println(b.equals(c));  //输出为false,可见数组并没有重写hashcode()和equals()方法16      System.out.println(Arrays.equals(b, c));  //利用java.util.Array的equals()来判断数组是否相等,这里输出true17      System.out.println(isEquals(b,c));18      19     }20 21     /**22      * 重写方法来手动实现数组之间的比较方法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 immutable. Arrays do not override object's hashcCode() and equals() methods.

We all know that arrays can also be two-dimensional. Let’s see how a two-dimensional array is declared:

1 int[][] da = new int[2][3];  //推荐用这种声明方式,更能表明数组的类型2 int db[][] = new int[4][3];

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

1 int[][] dc = new int[2][];  //第一维的大小不能空缺,第二维的大小可以是不一样的。2 dc[0] = new int[2];3 dc[1] = new int[3];

Okay, let’s stop talking about the application of the basic data structure of array. In order to highlight the theme, we will not talk about the other irrelevant applications. .

Next, let’s take a look at the ArrayList collection:

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 declaration:

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

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

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

 ArrayList<Integer> list =  ArrayList<Integer>         list.add(1         list.add(2         list.add(3         list.remove(1         Object[] p = list.toArray();           System.out.println(p[0         System.out.println(list.contains(4));           System.out.println(list.size());           System.out.println(list.get(0));

The above shows some important methods of ArrayList. Let's compare these two collection classes:

(1) ArrayList is a complex version of Array
ArrayList internally encapsulates an array of type Object. In a general sense, it is different from an array. The essential difference is that many methods of ArrayList, such as Index, IndexOf, Contains, Sort, etc., directly call the corresponding methods of Array based on the internal array.

(2) Stored data types

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

(3) Variable length

The length of Array is actually immutable. The actual length of a two-dimensional variable-length array is also fixed. What is variable is the length of the elements. . The length of ArrayList can be specified (even if the length is specified, it will be automatically expanded by 2 times) or not specified, and it is variable length.

(4) Accessing, adding and deleting 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 unboxing operations, and frequent operations may affect some efficiency. In addition, ArrayList is a dynamic array, which does not include algorithms for fast access through Key or Value, so in fact calling methods such as IndexOf and Contains is a simple loop to find elements, so calling such methods frequently is no better than writing it yourself. Looping and a little optimization can be done quickly. If you have this requirement, it is recommended to use a collection of key-value pairs such as Hashtable or SortedList.

Okay, that’s it for the comparison of these two data structures. If there is anything wrong that you feel is welcome, you are welcome to correct me.

In addition, this is my first time writing a blog, please be gentle.

The above is the detailed content of The difference and connection between Array and ArrayList. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:A simple java algorithmNext article:A simple java algorithm