List to ArrayList?

What is the point in having something like this:

List<String> listName = new ArrayList<String>();

I believe this is polymorphism, but what advantage does that give you?

now you may want to use an ArrayList

but depending on the data you may want to switch to like a LinkedList later on

so you can switch implementations - I wouldn’t recommend it, but its possible

If you for example create a list of lists there you want to do the same method.

One example is if you want to loop through a list of units with different movement methods and you only want to call the method .move() for each unit through a for loop.

IHMO there is no advantage.

It separates concept from implementation.

It allows you to swap the implementation without breaking any code. (just like the first reply stated)

I’m not sure what “IHMO” means, but:
There is certainly an advantage for libraries that change over time, but don’t want their users to rewrite their code every time a thing like implementation details are changed.

  • Scarzzurs

Like Cero said, it allows you to use a LinkedList or some other class that extends List without changing anything but the creation code. However, in the case of List, different implementations have widely different performance characteristics. Therefore there aren’t many opportunities to take advantage of this, as you’ll want to optimize your gets and add/sets depending on what kind of List you used in the first place.

I don’t know how much knowledge you have of Java (or object oriented programming), but take a look at this example:

Weapon weapon = new Dagger(); //Dagger extends Weapon

In this case, it is obvious that the weapon can be swapped for a completely different kind of weapon (like a Sword, Bow, Shotgun, PortableDeathStar, e.t.c. ;)), so it is important that the weapon variable can hold all different weapons in the game, or you’ll have to make an endless if-else chain to find which weapon you have equipped, and/or other terrible hacks.

It’s more useful to use List in a method signature or as part of the API that some other piece of code might use. Since someone else will be using that idea, you’ll have no idea how or where they’ll be getting their lists. Theoretically, they may have a custom list, or a thread-safe list, etc. and you’d want to support that. If all you needed was to iterate, you might even use Collection to support people passing in sets and queues, etc.

In this case, where it looks like you’re just using the list as a local variable, the polymorphism has less benefit because you have access to the entire code and if you need to change implementation choices, it’s easy enough to just change the type right then.

True, but it’s still very important to know what kind of list you’re operating on. Looping through a 1000 element ArrayList using a for-loop will be almost instant, but looping over a 1000 element LinkedList with get(index) is going to be insanely slow. Unless you know what kind of List you’re going to receive, you can’t write fast code handling it.

Nevermind me. A grumpy dyslexic demon that didn’t notice this was a newbie question.

Because of different iteration performances which were mentioned, one should of course use the Iterator of the list and not the get(i) methode.

When one uses the “new” (who is really still using java 1.4???) for loop (for(String a: string_collection)) u get the benefit of the Iterator and also clean code

In My Humble Opinion

then that should be “IMHO” :expressionless:

That the dyslexic part.

I should be hanging on internet longer >_>

Personally, I think the major interest in separating interfaces from implementations with lists is the use of Collections.unmodifiableList(). It allows you to convert your list into a sealed list that will throw a runtime exception everytime you try to modify it. Returning such list in a getter is absolutely necessary if your design shouldn’t allow the list to be modified, even if your the only user of your method.

Weird. Everyone I meet says the exact opposite thing.