4

I have a little question, about how to create Arrays with unknown size. I guess the best way is using the java.util.ArrayList; my question is, what's the difference between:

ArrayList client_Catalog = new ArrayList<>();

And

List<> list = new ArrayList<>();

Btw on my program I'm trying to create an empty list where I'm gonna store clients info, so, objects of that class, I don't know the final size.

2
  • For starters, neither compile...but you'll find solace in looking at "What is a raw type and why should we avoid it", as well as "Programming to the interface". Commented Nov 15, 2016 at 21:06
  • What the heck does the difference between those two lines have to do with creating arrays of unknown size? Commented Nov 15, 2016 at 21:31

3 Answers 3

2

There is no such thing as an array with unknown length. You either have an array with fixed length or a List which is flexible size.

side remark
Please remember that String[] can hold a reference to an array of Strings of any size and that array can be changed but you can't append it. You can only copy the previous content to a larger array and then fill the place which was left.

List is an Interface. ArrayList is one of it's implementations.

ArrayList client_Catalog = new ArrayList<>();

says: let the client_Catalog be an ArrayList and let it be empty first. On the other hand you have:

List<> list = new ArrayList<>();

which says let list be any implementation of a List. It can be ArrayList, LinkedList or whatever implements it. The only thing you have guaranteed is that that item will have all methods from List interface.

And now how to deal with generics. List is generic. One day, someone thought that there is no reason to implement List for each datatype and decided to introduce generics. You have to declare what type will this list be of. It's done with those <> brackets.

For example, to create ArrayList of Strings you write:

ArrayList<String> myList = new ArrayList<>();

As it's just an Q&A zone, I won't teach you everything, but my answer has some basics about Lists. I also decided to atttach links to some official tutorials and documentation to help you with learning. Just in case there is something you'd like me to extend, please ask in a comment.

Sign up to request clarification or add additional context in comments.

Comments

0

Any of the options is fine to serve your requirement. It's better to use List interface as return type of a method. In that case it will abstract the actual implementation of list, i.e., the caller of the method doesn't need to know the actual implementation.

Comments

-1

Between

ArrayList client_Catalog = new ArrayList<>();

and

List<> list = new ArrayList<>();

Will there not be any difference. Both will work and work out exactly as you wont. The only distinction is that in the second examples, one can not only have an ArrayList. But also for example LinkedList or other object that implements the List interface.

I would in your case, select the upper to ensure that it is always an ArrayList if you want to rely on it 100%.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.