The Artima Developer Community
Sponsored Link

Java Answers Forum
Multiple objects

3 replies on 1 page. Most recent reply: Mar 24, 2005 9:28 AM by John Neale

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
cBin

Posts: 13
Nickname: cbb
Registered: Mar, 2005

Multiple objects Posted: Mar 23, 2005 12:24 AM
Reply to this message Reply
Advertisement
Probably an easy question, but I am just learning.
I need to:
a- have a container that can hold a variable number of objects depending on the user actions, eg. every time the screen is clicked on the coordinates of the click point are added to a list, but this list can hold a "infinite" number of points.

b-create multiple objects, each with a different name (I think?) eg, in the above example, each mouse click causes a new object "x" to be created so that I have "x1,x2,..xn"

is there a good and or standard way to do this?

thanks
c


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Multiple objects Posted: Mar 23, 2005 2:57 AM
Reply to this message Reply
Use java.util.Vector .
Look at the API for documentation.
Just on hint: Vector stores instances of "Object", what means it can store any class.
But it does not know of what class the stored Object is.
So if later you want to access the Object, you must first cast it.
For example:
Point2D.Double p = new Point2D.Double(15, 16);
Vector v = new Vector();
v.add(p);
//the following line will result in a compile error, since you cannot assign Object to a Point2D.Double variable
Point2D.Double p2 = v.get(0);
//this is the correct way:
Point2D.Double p3 = (Point2D.Double)v.get(0);
//This is the way to acces a property directly
double x2 = ((Point2D.Double)v.get(0)).getX();


"different names" des not make much sense, since at runtime there exist no "names" for variables.
Maybe you need a description or something.

In this case create a class containing a description and a Point and add instances of this class to the vector.

For getting the coordinates add an actionlistener to the panel (you are using a panel, right?), ask for Mousclick and get the coordinates from the event variable.

cBin

Posts: 13
Nickname: cbb
Registered: Mar, 2005

Re: Multiple objects Posted: Mar 23, 2005 5:48 AM
Reply to this message Reply
Thanks, exactly what I need to know.

John Neale

Posts: 10
Nickname: rhino
Registered: Oct, 2003

Re: Multiple objects Posted: Mar 24, 2005 9:28 AM
Reply to this message Reply
Just a point. Vector is not usually the best choice for a collection class as all it's methods are synchronised and therefore slow (unless you need the synchronisation between threads). You are better to use a type of List.

An ArrayList is based by an array. If you want to grow the list very large then you should consider a LinkedList instead. Check out http://java.sun.com/docs/books/tutorial/collections/index.html for a Collections tutorial.

As for naming - each object you create is unique and will exist in the List as a unique object.

Flat View: This topic has 3 replies on 1 page
Topic: 2 quick ones Previous Topic   Next Topic Topic: one more Question

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use