aspose file tools *
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
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
Rancher

Joined: Mar 22, 2005
Posts: 42952
    
  73
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?
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: 40079
    
  28
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: 14445
    
  23

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 8 API documentation
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: 40079
    
  28
You're welcome.
nimo frey
Ranch Hand

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


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: vector or list or ?