#Lists vs. Arrays
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, 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>.
list. However, there are some key differences between lists and arrays.
-
Size: Arrays have a fixed size, meaning you need to specify the number of elements it can hold when you create it. On the other hand, lists can dynamically grow and shrink as needed.
-
Type: Arrays can hold elements of any type, including primitive types like int or char, as well as objects. Lists, specifically ArrayLists in this case, can only hold objects and not primitive types directly. However, there are wrapper classes available to convert primitive types into objects for use in lists.
-
Flexibility: Lists provide more flexibility in terms of adding or removing elements at any position within the list. Arrays do not have built-in methods for adding or removing elements easily; you would need to manually shift elements around to accommodate changes.
-
Performance: Arrays generally have better performance when it comes to accessing elements by index since they provide direct access to memory locations. Lists, on the other hand, require additional overhead due to their dynamic nature and use of object references.
-
Functionality: Lists offer a wide range of built-in methods for manipulating and working with collections of data such as sorting, searching, filtering, etc. Arrays have fewer built-in methods and require more manual coding for similar operations.
In summary, while both lists and arrays serve the purpose of storing collections of data, lists provide more flexibility and functionality compared to arrays but may come with some performance trade-offs.
TY ai 🤖
AI is correct here
Be careful of reading more into the AI response that is literally expressed. It implies things that aren't true in the general case.
eg
- an array is created as a container for a specific type (and assignment compatible subtypes). So any given array cannot hold elements of any type.
- ArrayList access is about the same cost as array access (for the same type of content), because the underlying storage of the ArrayList is actually an array.
- the Arrays class provides some operations that Lists don't (without consumption in a stream) - eg binary searching or parallel sorting arrays, finding first mismatch.
However it is, by and large, one of the better answers it's given. You'll generally find yourself using List/Set/Map/Stream more often than arrays.
If all you will be doing is setting and getting like list.get and list.set then you may aswell use an array
In general arrays are better than list if:
-
you need an int[] or boolean[] array (unboxed Integer and Boolean for example can be null unlike unboxed arrays though)
-
you want to make a 2d/3d array like int[][][] which may save a lot more time than working with List<List<List<Integer>>> and you can't use something like a vector or equivalent (cause vector classes and such make complex stuff easier)
those are excluding the times where you might need to remove
but if you want performance, arrays are literally only bits faster
unless you can use ways to improve performance significantly from a really slow program that only works using an array
(such as mentioned, parallel sorting that won't block the main thread thus everything on the main thread is faster, but parallel sorting will not make your code run faster, it will make other tasks not be blocked thus everything is done quicker but nothing actually gives more performance)
unless it can save time or
can make the code necessarily faster you would find yourself just using lists because they are more developer friendly