Array和ArrayList的差异

Difference between Array and ArrayList

Similarities

  • Array and ArrayList both are used for storing elements.
  • Array and ArrayList both can store null values.
  • They can have duplicate values.
  • They do not preserve the order of elements.

The following table describes the key differences between array and ArrayList:

BasisArrayArrayList
DefinitionAn array is a dynamically-created object. It serves as a container that holds the constant number of values of the same type. It has a contiguous memory location.The ArrayList is a class of Java Collections framework. It contains popular classes like Vector, HashTable, and HashMap.
Static/ DynamicArray is static in size.ArrayList is dynamic in size.
ResizableAn array is a fixed-length data structure.ArrayList is a variable-length data structure. It can be resized itself when needed.
InitializationIt is mandatory to provide the size of an array while initializing it directly or indirectly.We can create an instance of ArrayList without specifying its size. Java creates ArrayList of default size.
PerformanceIt performs fast in comparison to ArrayList because of fixed size.ArrayList is internally backed by the array in Java. The resize operation in ArrayList slows down the performance.
Primitive/ Generic typeAn array can store both objects and primitives type.We cannot store primitive type in ArrayList. It automatically converts primitive type to object.
Iterating ValuesWe use for loop or for each loop to iterate over an array.We use an iterator to iterate over ArrayList.
Type-SafetyWe cannot use generics along with array because it is not a convertible type of array.ArrayList allows us to store only generic/ type, that's why it is type-safe.
LengthArray provides a length variable which denotes the length of an array.ArrayList provides the size() method to determine the size of ArrayList.
Adding ElementsWe can add elements in an array by using the assignment operator.Java provides the add() method to add elements in the ArrayList.
Single/ Multi-DimensionalArray can be multi-dimensional.ArrayList is always single-dimensional.

原文地址:https://www.javatpoint.com/difference-between-array-and-arraylist

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值