#what is the difference between the functionality of an Array and an arrayList in Java?
1 messages ยท Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question ๐ค. Maybe it helps! In any case, a human is on the way ๐. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
An Array in Java is a fixed-size data structure that stores elements of the same data type in contiguous memory locations. Once created, the size of an array cannot be changed. Elements can be accessed using their index positions.
On the other hand, ArrayList in Java is a dynamic data structure that can grow or shrink in size as needed. It is implemented using an underlying array that gets resized automatically when elements are added or removed. Elements can be accessed using methods like get() and set().
Some key differences between Array and ArrayList include:
-
Size: Arrays have a fixed size determined at the time of creation, while ArrayLists can dynamically resize themselves.
-
Type: Arrays are type-safe and can only store elements of a specific data type specified at the time of creation, while ArrayLists can store objects of any type.
-
Methods: Arrays have limited functionality and require manual coding for operations like resizing or adding/removing elements, while ArrayLists provide built-in methods for these operations.
In summary, Arrays provide a simple and efficient way to store fixed-size collections of elements, while ArrayLists offer more flexibility and functionality for dynamic collections that need to grow or shrink during runtime.
Array is a non-resizable segment of memory
ArrayList is an implementation of a List that uses an array under the hood, if it gets full and you add another element to it, a new, bigger aray is allocated and the old one is discarded
then when should I use array over arraylist?
when you have a constant collection of things that never changes
also take care to note that arrays are always mutable
people nowadays often use arraylist where an array would work as well
you shouldn't expose them
do u want to model a parking space for cars with a fixed amount of exactly 4 slots? then array
do u want to model sth that can change in size (increase or decrease), for example a shopping list? then a list
and when u want to pick a list, u can choose between multiple implementations of lists. for example arraylist, linkedlist - from which ud typically choose arraylist
so arraylist is an array that changes in size?
thats not a thing
arrays are fixed sized
a list is a datastructure that can grow and shrink in size (unlike arrays)
and arraylist specifically is a list that uses arrays under the hood to fulfil its job as list
just like u use a computer to do ur work
ur a human, not a computer
just bc u use one
ur a Computer(Using)Human, so to speak
๐