📜  Java中的数组与数组列表

📅  最后修改于: 2022-05-13 01:55:18.065000             🧑  作者: Mango

Java中的数组与数组列表

让我们在标题中简要讨论数组ArrayList的概念,以结合对Java程序的理解,以便稍后登陆它们之间的决定性差异。众所周知,数组是线性数据结构,提供了在内存地址空间中以连续方式添加元素的功能,而 ArrayList 是属于 Collection 框架的类。作为一名优秀的程序员,尽管知道这两者之间的区别,但他已经意识到在数组上使用 ArrayList。现在继续前进,即使使用 ArrayList 也有一个功能可以传递应该存储在 ArrayList 中的元素的数据类型类型,可以是对象、字符串、整数、双精度、浮点数等。

创建数组的方法

在Java中,以下是创建数组的两种不同方法。

  1. 简单的固定大小数组
  2. 动态大小的数组
int arr[] = new int[10]   

语法:声明一个静态数组数组

它可以进一步定义为两种类型:

  • 类型 1 :同时声明和初始化
  • 类型 2:声明而不是稍后初始化元素。

类型 1

Type array_name [array_size] ;
Type array_name = { Element1, Element2, Element3, Element4,...., ElementN } ;
// It is preferable if we have very limited array elements 

类型 2

int arr [100] ;
// This does means we are declaring a memory block named 'arr' 
// which is containing continuous 100 block associated in it

现在让我们详细讨论 ArrayList 的下一个概念,如下所示

语法:声明一个 Arraylist

Arraylist al = new ArrayList ;
// Here Type is the type of elements in ArrayList to be created

现在让我们借助 Array 和 ArrayList 之间的差异来说明示例

Base 1:数组是Java提供的基本功能。 ArrayList 是Java中集合框架的一部分。因此使用 [] 访问数组成员,而 ArrayList 有一组方法来访问元素并修改它们。

例子:

Java
// Java program to demonstrate differences between
// Array and ArrayList
 
// Importing required classes
import java.util.ArrayList;
import java.util.Arrays;
 
// Main class
class GFG {
   
    // Main driver method
    public static void main(String args[])
    {
        // Input array
        int[] arr = new int[2];
        arr[0] = 1;
        arr[1] = 2;
 
        // Printing first element of array
        System.out.println(arr[0]);
 
        // ArrayList
        // Creating an arrayList with initial capacity
        // say bi it 2
        ArrayList arrL = new ArrayList(2);
 
        // Adding elements to ArrayList
        // using add() method
        arrL.add(1);
        arrL.add(2);
 
        // Printing alongside accessing
        // elements of ArrayList
        System.out.println(arrL.get(0));
    }
}


Java
// Java program to demonstrate differences between
// Array and ArrayList
 
// Importing required classes
import java.util.ArrayList;
import java.util.Arrays;
 
// Main class
class GFG {
    // Main driver method
    public static void main(String args[])
    {
        // Normal Array
        // Need to specify the size for array
        int[] arr = new int[3];
        arr[0] = 1;
        arr[1] = 2;
        arr[2] = 3;
 
        // We cannot add more elements to array arr[]
 
        // ArrayList
        // Need not to specify size
 
        // Declaring an Arraylist of Integer type
        ArrayList arrL = new ArrayList();
 
        // Adding elements to ArrayList object
        arrL.add(1);
        arrL.add(2);
        arrL.add(3);
        arrL.add(4);
 
        // We can add more elements to arrL
 
        // Print and display Arraylist elements
        System.out.println(arrL);
        // Print and display array elements
        System.out.println(Arrays.toString(arr));
    }
}


Java
import java.util.ArrayList;
class Test
{
    public static void main(String args[])
    {
       // allowed
        int[] array = new int[3];
 
        // allowed, however, need to be initialized
        Test[] array1 = new Test[3];
 
        // not allowed (Uncommenting below line causes
        // compiler error)
        // ArrayList arrL = new ArrayList();
 
        // Allowed
        ArrayList arrL1 = new ArrayList<>();
        ArrayList arrL2 = new ArrayList<>();
        ArrayList arrL3 = new ArrayList<>();
       
        System.out.println("Successfully compiled and executed");
    }
}

输出
1
1

基数 2:数组是固定大小的数据结构,而 ArrayList 不是。在创建对象时无需提及 ArrayList 的大小。即使我们指定了一些初始容量,我们也可以添加更多元素。

例子:

Java

// Java program to demonstrate differences between
// Array and ArrayList
 
// Importing required classes
import java.util.ArrayList;
import java.util.Arrays;
 
// Main class
class GFG {
    // Main driver method
    public static void main(String args[])
    {
        // Normal Array
        // Need to specify the size for array
        int[] arr = new int[3];
        arr[0] = 1;
        arr[1] = 2;
        arr[2] = 3;
 
        // We cannot add more elements to array arr[]
 
        // ArrayList
        // Need not to specify size
 
        // Declaring an Arraylist of Integer type
        ArrayList arrL = new ArrayList();
 
        // Adding elements to ArrayList object
        arrL.add(1);
        arrL.add(2);
        arrL.add(3);
        arrL.add(4);
 
        // We can add more elements to arrL
 
        // Print and display Arraylist elements
        System.out.println(arrL);
        // Print and display array elements
        System.out.println(Arrays.toString(arr));
    }
}
输出
[1, 2, 3, 4]
[1, 2, 3]

基数 3:一个数组可以包含原始数据类型以及类的对象,具体取决于数组的定义。但是,ArrayList 仅支持对象条目,不支持原始数据类型。

例子:

Java

import java.util.ArrayList;
class Test
{
    public static void main(String args[])
    {
       // allowed
        int[] array = new int[3];
 
        // allowed, however, need to be initialized
        Test[] array1 = new Test[3];
 
        // not allowed (Uncommenting below line causes
        // compiler error)
        // ArrayList arrL = new ArrayList();
 
        // Allowed
        ArrayList arrL1 = new ArrayList<>();
        ArrayList arrL2 = new ArrayList<>();
        ArrayList arrL3 = new ArrayList<>();
       
        System.out.println("Successfully compiled and executed");
    }
}
输出
Successfully compiled and executed

Base 4:由于无法为原始数据类型创建 ArrayList,因此 ArrayList 的成员始终是对不同内存位置的对象的引用(有关详细信息,请参阅this)。因此,在 ArrayList 中,实际对象永远不会存储在连续的位置。实际对象的引用存储在连续的位置。

另一方面,在数组中,这取决于数组是原始类型还是对象类型。在原始类型的情况下,实际值是连续的位置,但在对象的情况下,分配类似于 ArrayList。 Java ArrayList 支持许多附加操作,如 indexOf()、remove() 等。Arrays 不支持这些函数。

我们已经实施并从输出中看到了它们之间的差异。现在让我们通过以表格格式绘制结论性差异来结束这篇文章,如下所示:

BaseArrayArrayList
Dimensionality It can be single-dimensional or multidimensional It can only be single-dimensional 
Traversing Elements For and for each generally is used for iterating over arrays Here iterator is used to traverse riverArrayList 
Length length keyword can give the total size of the array.size() method is used to compute the size of ArrayList.
SizeIt is static and of fixed lengthIt is dynamic and can be increased or decreased in size when required.
SpeedIt is faster as above we see it of fixed sizeIt is relatively slower because of its dynamic nature 
Primitive Datatype StoragePrimitive data types can be stored directly unlikely objectsPrimitive data types are not directly added unlikely arrays, they are added indirectly with help of autoboxing and unboxing
GenericsThey can not be added here hence type unsafe They can be added here hence makingArrayList type-safe. 
Adding Elements Assignment operator only serves the purposeHere a special method is used known as add() method