• Post Reply Bookmark Topic Watch Topic
  • New Topic

java collection with objects  RSS feed

 
Tamilselvan Kalimuthu
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a beginner in java... how to use collections with objects using getter setter methods..
i need to store more than two different type of values in collection object... like.. for example.. multiple (x,y) points, distance and with index values.
how to do that... any ideas?
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
You might find the Java tutorials easier to read.
If you can make all your objects instances of a common superclass, it will be much easier to give a generic type to your collection.
 
Safi Khan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use it like this:

consider the following class.

public class MyObj {
int x;
double y;

public MyObj(){}

void setX(int x){
this.x = x;
}

int getX() {
return x;
}

void setY(double y){
this.y = y;

double getY() {
return y;
}

}


Now to use it with collection you can do

ArrayList<MyObj> objInstance = new ArrayList<MyObj>();

Now you have a whoke collection of objects, use them :-)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tamilselvan Kalimuthu wrote:i need to store more than two different type of values in collection object... like.. for example.. multiple (x,y) points, distance and with index values.
how to do that... any ideas?

My suggestion - Unless, as Campbell suggested, these type are related: Don't.

A collection of unrelated objects is just that: a bunch of Objects; and there's very little you can do with them.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tamilselvan Kalimuthu wrote:
i need to store more than two different type of values in collection object


That's usually a sign of a design flaw. A collection should hold one type of object.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!