What is difference between array and ArrayList?

杀马特。学长 韩版系。学妹 提交于 2020-08-25 02:12:23

问题


What is an array and what is an ArrayList? What is the difference between them?


回答1:


An Array is a high performance way of storing a group of data with the same type because each element is laid out next to it's neighbor in memory. This allows for very fast access because (a) the code can do a little math and jump quickly to any location in the array, and (b) the elements are all grouped together so they tend to be in memory at the same time (fewer page faults and cache misses). An array in .NET is actually a class (System.Array), but a special type of class that is well understood by the .NET engine (CLR). Because of this, you can standard array access notation (text languages) such as foo[3] = 99;

In ArrayList, however, you are dealing with a collection. There are several types of collections in .NET (see the System.Collections and System.Collections.Specialized namespaces), but the key thing about them is the interfaces they support (IEnumerable, ICollections, IList, etc). If you look at the definition of these interfaces, you see that collections are all about grouping things together and providing methods to access them.With an ArrayList, however, if you add one element more than the internal array can handle, the ArrayList automatically creates a larger array and copies the old array into the new array.




回答2:


Difference between Array and ArrayList are following:

  1. Implementation of array is simple fixed sized array but Implementation of ArrayList is dynamic sized array.
  2. Array can contain both primitives and objects but ArrayList can contain only object elements
  3. You can’t use generics along with array but ArrayList allows us to use generics to ensure type safety.
  4. You can use *length *variable to calculate length of an array but size() method to calculate size of ArrayList.
  5. Array use assignment operator to store elements but ArrayList use *add() *to insert elements.



回答3:


array : similar data typed elements and the size is limited. arraylist : is a collection which is capable of saving different data typed objects,And is growable.




回答4:


is this for Java right? Well, I read somewhere that 1. ArrayList is a collection datatype which takes value/key pair. You need to use .Add property to add values to the arraylist collection. You can access the arraylist using keys. 2. Array is a datatype which can be accessed using indexes.




回答5:


i, Array:Array is static in size that is fixed length data structure, One can not change the length after creating the Array object. Arraylist :ArrayList is dynamic in size . Each ArrayList object has instance variable capacity which indicates the size of the ArrayList. As elements are added to an ArrayList its capacity grows automatically. 2.Array :it can only contains Object while Array can contain both primitive data types as well as objects. ArrayList:ArrayList can not contains primitive data types (like int , float , double) eg : ArrayList arraylistobject = new ArrayList(); arraylistobject.add(23); 3.Array : the length variable which returns the length of the array. ArrayList :Length of the ArrayList is provided by the size() method while Each array object .




回答6:


Array: Array is a fixed length data structure. You can not change length of Array once created in Java. Array is static in nature.

Object[] objArray = new Object[10];

ArrayList: ArrayList is a variable length Collection class. ArrayList re-size itself when gets full depending upon capacity and load factor. Arraylist is Dynamic in nature.

ArrayList<Integer> integerList = new ArrayList<Integer>();



回答7:


Talking about memory optimization -

Array - It is better as whole block of memory is stored at one particular location.

ArrayList - It is a bit low because if an arraylist has size say 100 and we need to add 1 more element to it , it will then search for a memory space of 101 and then allocate the fresh memory. For low numbers like 100 it wont create much difference but for larger number it can cause low performance.



来源:https://stackoverflow.com/questions/1552742/what-is-difference-between-array-and-arraylist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!