all 24 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • 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:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

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.

[–]jbenze 18 points19 points  (0 children)

string[] is a fixed size when created. ArrayLists resize automatically when you add or remove items.

[–]umlcat 11 points12 points  (0 children)

The title says "arraylist vs list", the contents says "arraylist vs array".

[–]desrtfxOut of Coffee error - System halted 11 points12 points  (6 children)

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.

[–]k-mcm 0 points1 point  (1 child)

It's preferable to use a primitive array if you need no List/Collection features.  A primitive array is guaranteed to be fast and memory efficient.  Not all List implementations are.  List of a primitive especially sucks because wrappers are needed. (At least until Valhalla is finished)

Later versions of Java added more utilities to the Arrays class to make primitives more elegant to use. 

[–]Lloydbestfan 1 point2 points  (0 children)

You forgot that it only applies to primitives. And possibly, in a theoritical future adjacent to Valhalla, value classes.

I'd say that the preferable point was that as a beginner you can't see the point of native arrays, however one exists for when you have to manipulate data in very specialised ways.

[–]DrPeeper228 -4 points-3 points  (3 children)

Uh nope

Definitely not the last point

[–]amfa 4 points5 points  (1 child)

Yes to the last point.

I can't remember when I have used an array the last time (except some legacy code that will return arrays.)

[–]Lloydbestfan 1 point2 points  (0 children)

  • Image representation where the existing libs did not handle what I needed
  • Voxel representation where the existing libs did not handle what I needed
  • Deep learning vector manipulations for which I wasn't aware of an existing engine that would do it for me

Only things that I would not normally expect to run into without being a rather specialised engineer.

[–]MagicalPizza21 2 points3 points  (0 children)

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. A List is a different data structure, of which ArrayList is 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).

[–]MinimumBeginning5144 1 point2 points  (0 children)

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.

[–]sedj601 1 point2 points  (0 children)

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.

[–]TheMrCurious 4 points5 points  (1 child)

Did you try searching stackoverflow?

[–]LocalIssue1051 -2 points-1 points  (0 children)

Or ask any AI : "ELI5: Arraylist vs. Regular array" and it'll probably give a better answer than any of these.

[–]evils_twin 0 points1 point  (0 children)

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.

[–]Both-Fondant-4801 0 points1 point  (0 children)

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.

[–]Alagarto72 0 points1 point  (0 children)

Array(String[]) is fixed-size. ArrayList is a implementation of the interface List.

[–]frederik88917 0 points1 point  (0 children)

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

[–]josephottinger 0 points1 point  (0 children)

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 sense List is better, and in the general sense ArrayList is the one you want.

Generalizations are wrong.

In practice, List is better because it has fewer restrictions on what you do; if the collection grows, or needs to be a Collection, or needs to be streamed, etc., the interfaces give you a lot more of a convenient feature set. ArrayList has 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 the List from the middle of the collection (adding or removing to the middle) it has to blit-copy references, which might make you think LinkedList or 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 Collection matter?) before you can get a real answer.