Question:
What is the difference between an array and an array list in java?
anonymous
2013-03-15 06:11:06 UTC
I’m looking to store information in an array such as “Venue: Metro” but im not too sure how to store written information. Any sources or examples would be really appreciated.
Four answers:
Almighty Wizard
2013-03-15 06:20:27 UTC
An array is a simple data structure that holds a fixed number of values of a single type. Because it holds a fixed number of values, or elements, you cannot change the size of the list. This means you cannot remove an element from the array, you can just blank out its value, but its place in the array will remain. This is the easiest and quickest way to store data that you already know about.



An array list is a data structure based on the List interface of Java. The underlying data structure of the array list is an array, but they only way to access the data is through the publish List interface. While this may seem more cumbersome because you have to go through extra methods to get to data, it is easier to use in the sense that you can add/remove items from the list and the list will change size accordingly. You can also easily insert items into the middle of lists easier. Array lists, and lists in general, are much nicer to use when you are dealing with data you do not know about ahead of time and are required to build the datasets dynamically.
husoski
2013-03-15 07:34:04 UTC
The short and simple answer is that arrays are for fixed-length lists, and ArrayList objects are for variable-length collections. Both allow efficient access to elements by position (index), using someArray[index] for arrays, or someArrayList.at(index) for ArrayList objects.



An array is a fixed number of elements, stored sequentially so that elements can be quickly accessed by an index. Once created, the length of an array can never change. The elements of an array must all be of the same type, and that type can be either a primitive type (int, double, char, etc.) or an object type (class, interface, enum, or another array).



ArrayList is a class type (available in generic and "plain" versions). Either way, an ArrayList is an object that models a variable length array. New elements can be added or inserted, and existing elements can be deleted, changing the length of the array, with remaining elements shifted as needed so that the index values are always sequential and start with 0.



Array lists can only store object types, but each of the primitive types have a corresponding "wrapper" class, and the compiler provides automatic "boxing" conversions to convert primitive values to an objects when needed, and "unboxing" conversions to go the other way.



A plain ArrayList can store elements of (almost) any object type. A generic ArrayList can store only object of type SomeType or of a type that (directly or indirectly) extends SomeType. A generic ArrayList accepts objects of any type that implements the SomeInterface interface. The "almost" is that you can't use array types, even though they are objects, because they don't have class names. (There's an internal class name beginning with a '[' character, but that's not a Java identifier that you can use in a generic type.)
jarod
2016-12-17 22:58:40 UTC
An arraylist is a thread-volatile java answer for a dynamic array, an array is in basic terms an array of things that could nicely be dynamically allotted however the demands abit greater coding than ArrayList's. oftentimes an array is in common terms a static array like int[] i = new int[5];
?
2013-03-15 06:18:50 UTC
An array is a datatype that can hold a fixed ammount of objects of a certain datatype. Let's say we declare a string array with size 5:



String[] array = new String[5];



Then we would be able to store 5 String objects in this array. We can store less, but we cannot store more.



ArrayLists are similar to arrays, but they don't have a maximum size. If we declare an ArrayList for String objects like this:



ArrayList arrayList = new ArrayList();



Then we would be able to store any ammount of String objects in this.



ArrayLists actually use arrays, but the ArrayList replaces the array with a bigger one when needed. This process makes an ArrayList slightly slower for certain operations. If you only require a fixed ammount of objects in a collection, use an array. If you have no idea how many objects you need to store in a collection, use the ArrayList.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...