Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

vector or list or ?

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that would works, but is there another possibiltity without creating a class - only via using Datastructures:

Something like List<Object[]>
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, it s reasonable to do a class:

List<MyClass> ...


thanks

 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you!


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic