#what is the difference between the functionality of an Array and an arrayList in Java?

1 messages ยท Page 1 of 1 (latest)

lofty coyote
#

seen it multiple times, but i dont fully grasp the concept

buoyant groveBOT
#

<@&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>.

#
TJ-Bot
Seen it multiple times, but i dont fully grasp the concept

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:

  1. Size: Arrays have a fixed size determined at the time of creation, while ArrayLists can dynamically resize themselves.

  2. 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.

  3. 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.

wild bronze
rancid ingot
#

then when should I use array over arraylist?

wild bronze
#

when you have a constant collection of things that never changes

#

also take care to note that arrays are always mutable

rapid pilot
#

people nowadays often use arraylist where an array would work as well

wild bronze
#

you shouldn't expose them

desert perch
# rancid ingot then when should I use array over arraylist?

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

rancid ingot
desert perch
#

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

rancid ingot
#

alright

#

I still didn't go over arraylist

#

just started learning arrays

desert perch
#

๐Ÿ‘Œ