• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

java collection with objects  RSS feed

 
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?
 
Marshal
Posts: 60136
188
  • 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.
 
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 :-)
 
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
 
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!