• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

About vectors  RSS feed

 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I was wondering if it is possible:
I have Vector v and it is empty. Now i want to create 5 Point objects that are part of this vector, so that first element of this vector is point0, second one is point2.
So that v.elementAt(4)=point4;

And i want to make new point objects dynamically for example like so:


but i think it is not possible.
[ August 09, 2004: Message edited by: Juhan Voolaid ]
 
author and iconoclast
Posts: 24203
43
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Creating the Points and adding them to the vector is easy, but you can't define new variables this way. The good news, however, is that you don't need to -- there's nothing you could do with "point0" that you couldn't do with

(Point) vector.get(0)

By the way: note that your for-loop creates six points, not 5. In general, to count to N in a for-loop, you say

for (int i=0; i<N; ++i)
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK ... I thought so, but still i have problems.
At first i try to make a vector of points and then access to one point.
I get error that cannot resolve symbol method getX();

that's what i did:


I don't know what is wrong here.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
43
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
v.get(1) (you can and should just use "get()" instead of "getElementAt()") returns an Object. You need to cast this Object to a Point,

(Point) v.get(1)

then put parentheses around the whole thing:

((Point) v.get(1))

and then call methods on the whole parenthesized expression, like this:

((Point) v.get(1)).getX()


Then you can print the result


System.out.println(((Point) v.get(1)).getX());


Now, note that you can also do this:

Point p = (Point) v.get(1);
System.out.println(p.getX());

You could reuse "p" to refer to all of the points in turn, one at a time.
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx, that made thing clare to me but still one question;

if i want to change one point's cordinates I do like this:
but it gives not a statement error;
int x, y;

and if i ...

cannot resolve symbol method

help
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
43
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again,

v.get(0)

is an Object, while

((Point) v.get(0))

is a Point (including all the parentheses). So you have to say

((Point) v.get(0)).setLocation(x, y+10);

If you leave out that outer set of parentheses, then the case applies to the whole expression, and the compiler will look for the setLocation method in the Object class.
 
Do not set lab on fire. Or this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!