*
The moose likes Beginning Java and the fly likes vector or list or ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "vector or list or ?" Watch "vector or list or ?" New topic
Author

vector or list or ?

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I need a Datastructure, that can hold various datatypes.

for example:

myContainer has a Array of {(1,1 "hello"), (1,2 "bye"), ..}

Is that Datastructure right or should I use other kinds?




Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41087
    
  43
A List can only hold a single type, not multiple types. How about creating a class that has fields for those 3 types, and then keeping that in the List?


Ping & DNS - my free Android networking tools app
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Yes, that would works, but is there another possibiltity without creating a class - only via using Datastructures:

Something like List<Object[]>
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
You want a class which can encapsulate a long, a float and a String, then create a List of that class.
Oh, that's what Ulf has already said. Sorry.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

You could ofcourse use List<Object[]> but that would not be really type-safe. If the things you want to store are always triplets of Long, Float and String, then when you use a List<Object[]> the compiler will not be able to check that for you, and you might inadvertently put an Object[] into the list that contains something else than a Long, Float and String.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Okay, it s reasonable to do a class:

List<MyClass> ...


thanks

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
You're welcome.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
thank you!


 
Consider Paul's rocket mass heater.
 
subject: vector or list or ?
 
Similar Threads
float insted of double
question from nikos blog(generics)
Wild card ? extends Number in return of a method ?
[SOLVED] Syntax error on token
generics Strike again !!!