It seems to me like ArrayList would be easier to use in nearly every scenario, it being very versatile. Is there an instance where a String[] would be used to store inputted data? If there is such a case, there must be a drawback in ArrayList, what would that be?
Only thing that comes to mind off the top of my head would be the variety of String methods like, substring() and split(), etc.
EDIT: New to StackOverflow as well. I apologize if this was a re-post. And thanks for the formatting.
The short answer is: don’t use an array, always use an array list. The only exception to this is if you absolutely need to control the amount of memory used or you have some specific performance constraint that only
String[]can support.In general, though, arrays are terrible to work with in an object oriented language, and almost always for the same reason: they make it very easy to break encapsulation. For example:
See any problems? Yea, you need to write
getStrings()like this:Otherwise, some caller can get a hold of your class’ internal data and start modifying it. The only way to prevent this is to copy it every time a caller wants to see it. As opposed to the right way:
Now you’re not copying the data, you’re just sticking it behind an API that doesn’t allow changes. If you use the Guava
Immutable*classes, even better.