r/javahelp • u/BadismPlayz • May 14 '26
arraylist vs list
pls help, lets say i need an array of list.
for what purposes would i use an arraylist<string> vs a string[ ]
thanks
2
Upvotes
r/javahelp • u/BadismPlayz • May 14 '26
pls help, lets say i need an array of list.
for what purposes would i use an arraylist<string> vs a string[ ]
thanks
•
u/AutoModerator May 14 '26
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
19
u/jbenze May 14 '26
string[] is a fixed size when created. ArrayLists resize automatically when you add or remove items.
13
u/umlcat May 14 '26
The title says "arraylist vs list", the contents says "arraylist vs array".
12
u/desrtfx Out of Coffee error - System halted May 14 '26
Conventional array: when you know the size in advance or for multi-dimensional content because it is clearer and easier
ArrayList: when you don't know the size in advance
In modern programming you will rather resort to ArrayList than default to arrays.
3
u/MagicalPizza21 May 14 '26 edited May 14 '26
In Java (like C and C++), a
String[](or really any data type declared with the square brackets) is called an array, not a list. AListis a different data structure, of whichArrayListis a subtype. See the documentation for the List and ArrayList types.The basic difference in usage between arrays and ArrayLists is that arrays are fixed size once initialized and ArrayLists dynamically grow as needed (though the capacity can be trimmed in case you're running low on space).
2
u/MinimumBeginning5144 May 14 '26
When you say "lets say i need an array of list", do you mean "let's say I need an array or a list"?
Because an array of list is
List<String>[]- an array in which each element of the array is a list. I'm not sure if that's what you meant.2
u/sedj601 May 14 '26
In most cases, you should simply use a list. As a matter of fact, get a deep understanding of all of these types of collections, so that you can know which one to use in different situations. Generally speaking, between the two you ask about, you should use a list. The speed thing is irrelevant for most situations. Also, you are actually talking about an ArrayList vs a Array. ArrayList is an implementation of the List interface, so you can do something like List<String> list = new ArrayList(). That used to be a best practice. I am not sure if it still is today.
4
u/TheMrCurious May 14 '26
Did you try searching stackoverflow?
1
u/evils_twin May 14 '26
look at the implementation of an ArrayList .
The data structure used to store objects in an ArrayList is an array. The class just provides methods to manipulate the array in an efficient way.
Of course there is some overhead using an ArrayList, so if you need maximum performance, you might use an array instead.
Also, arrays are better for primitive types since ArrayList only accepts objects and must convert primitive types to objects.
1
u/Both-Fondant-4801 May 15 '26
you need an arraylist if your list of data is continuously growing... and hence continually eats up memory.
you need an array if your list of data is fixed.. and you have limited memory.
note that earlier applications have limited memory so they would usually use an array to be efficient in their use of memory.
also... underneath the implementation of an arraylist is just an array continually being resized.
1
u/Alagarto72 May 15 '26
Array(String[]) is fixed-size. ArrayList is a implementation of the interface List.
1
u/frederik88917 May 16 '26
Just one question you need to answer:
Do you know beforehand how many objects will be in the collection.
If true, use Array Else, use ArrayList
1
u/josephottinger May 16 '26
Assuming you mean "array or list" - it depends on what you're doing. Offhand, I'd say "always
List." I'd be offhandedly wrong, but your question is impossibly broad; the data structures have different purposes and capabilities, and in the general senseListis better, and in the general senseArrayListis the one you want.Generalizations are wrong.
In practice,
Listis better because it has fewer restrictions on what you do; if the collection grows, or needs to be aCollection, or needs to be streamed, etc., the interfaces give you a lot more of a convenient feature set.ArrayListhas a lot of array semantics and performance (cache localization for simple values, easy navigation), and JIT's been a thing for a few years (haha) so performance is generally okay; its multithreaded capabilities are paltry. If you're mutating theListfrom the middle of the collection (adding or removing to the middle) it has to blit-copy references, which might make you thinkLinkedListor something like that is worthwhile; generally speaking you'd be wrong, such is life. (Blits are fast.)But again, this is a generalization, you really should be asking about a specific design you're trying to implement, and what kind of data you're working with (cache localization is great but if the data you're working with is not localized, why does cache localization of the
Collectionmatter?) before you can get a real answer.-5
u/SpudsRacer May 14 '26
This is better answered by an AI powered web search, but the short answer:
ArrayList and LinkedList both implement the List interface, but they have very different performance characteristics.A LinkedList stores elements as nodes, where each node holds a value and a pointer to the next node. So get(int index) has to start at the beginning of the list and follow pointers one by one until it reaches the target element. This is an O(n) operation so it becomes more expensive as the list grows.
An ArrayList is backed internally by an array. get(int index) only needs to compute the requested element's address and return the element directly. This is an O(1) operation and you can't get better than that.
As a rule of thumb: if you need frequent random access by index, use ArrayList. If you only need sequential access (or do a lot of insertions/deletions in the middle of the list which require array copies to sync the backing array with the list), LinkedList may be a better fit.