Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (527)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (594)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  List to ArrayList?  (Read 2356 times)
0 Members and 1 Guest are viewing this topic.
Offline duce

Senior Newbie





« Posted 2011-12-17 16:42:30 »

What is the point in having something like this:
1  
List<String> listName = new ArrayList<String>();

I believe this is polymorphism, but what advantage does that give you?
Offline Cero
« Reply #1 - Posted 2011-12-17 16:51:47 »

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

Offline Skarion

Senior Devvie


Medals: 2
Projects: 1



« Reply #2 - Posted 2011-12-17 17:18:51 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen
« Reply #3 - Posted 2011-12-17 17:55:37 »

IHMO there is no advantage.
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 834
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2011-12-17 18:00:38 »

It separates concept from implementation.

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Scarzzurs
« Reply #5 - Posted 2011-12-17 18:02:46 »

IHMO there is no advantage.

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

My games and Projects:
BlastingPixels.com,
Old website
Offline theagentd

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #6 - Posted 2011-12-17 18:42:34 »

What is the point in having something like this:
1  
List<String> listName = new ArrayList<String>();

I believe this is polymorphism, but what advantage does that give you?
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:
1  
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.  Wink), 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.

Myomyomyo.
Offline lhkbob

JGO Knight


Medals: 32



« Reply #7 - Posted 2011-12-17 18:47:12 »

It's more useful to use List<X> 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<X> 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.

Offline theagentd

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #8 - Posted 2011-12-17 18:50:38 »

It's more useful to use List<X> 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<X> 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.

Myomyomyo.
Offline Roquen
« Reply #9 - Posted 2011-12-17 20:47:49 »


Nevermind me.  A grumpy dyslexic demon that didn't notice this was a newbie question.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Danny02
« Reply #10 - Posted 2011-12-18 00:37:05 »

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
Offline sproingie

JGO Kernel


Medals: 202



« Reply #11 - Posted 2011-12-18 05:33:40 »

I'm not sure what "IHMO" means

In My Humble Opinion
Offline ReBirth
« Reply #12 - Posted 2011-12-18 05:53:19 »

then that should be "IMHO" :|

Offline Roquen
« Reply #13 - Posted 2011-12-18 07:02:31 »

That the dyslexic part.
Offline ReBirth
« Reply #14 - Posted 2011-12-18 21:59:37 »

I should be hanging on internet longer >_>

Offline AurelienRibon

Senior Newbie


Medals: 3


Toolmaker, yeah.


« Reply #15 - Posted 2011-12-19 14:05:59 »

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.

Offline theagentd

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #16 - Posted 2011-12-19 15:30:00 »

I should be hanging on internet longer >_>
Weird. Everyone I meet says the exact opposite thing.

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

PocketCrafter7 (14 views)
2014-11-28 16:25:35

PocketCrafter7 (10 views)
2014-11-28 16:25:09

PocketCrafter7 (11 views)
2014-11-28 16:24:29

toopeicgaming1999 (76 views)
2014-11-26 15:22:04

toopeicgaming1999 (67 views)
2014-11-26 15:20:36

toopeicgaming1999 (17 views)
2014-11-26 15:20:08

SHC (30 views)
2014-11-25 12:00:59

SHC (28 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (28 views)
2014-11-24 19:59:16
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!