wood burning stoves*
The moose likes Java in General and the fly likes creating variables in a loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "creating variables in a loop" Watch "creating variables in a loop" New topic
Author

creating variables in a loop

Dalibor Toth
Ranch Hand

Joined: Jul 16, 2001
Posts: 38
Can anybody tell me how to dinamically create vectors. Here's the idea
int numcols = 4;
for (int i = 0; i < numcols; i++) {
//create 4 vectors
Vector v + i = new Vector();
}
and the outcome needs to be something like:
v1
v2
v3
v4
Please, please, PLEASE!!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Take a look at arrays.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Dalibor Toth
Ranch Hand

Joined: Jul 16, 2001
Posts: 38
sorry, need vectors!
BTW, I took a look into arrays but could not find anything that could help. Can you post some algorythm?
Peter Kristensson
Ranch Hand

Joined: Jul 02, 2001
Posts: 118
Ok, you need vectors.
but an array of vectors, eh?
/peter
Dalibor Toth
Ranch Hand

Joined: Jul 16, 2001
Posts: 38
You'd still need to initialize those (f...) vectors.
In html page that has applet tag calling my applet, there is a param tag saying how many columns do I have (say, 4). The applet reads the tag and initializes as much vectors as the param tag says - so in this case - 4. If I would set that parameter to 7, the applet would create 7 vectors. Etc.
Can somebody, please, post some code? It would be appreciated.
Dave Van Even
Ranch Hand

Joined: Jul 19, 2001
Posts: 101
int numcols = 4;
Vector v[] = new Vector[numcols];
for (int i = 0; i < numcols; i++) {
//create 4 vectors
Vector v[i] = new Vector();
}
??? is this what you want ??
Dalibor Toth
Ranch Hand

Joined: Jul 16, 2001
Posts: 38
I get these errors:
sc.java:35: ']' expected
Vector v[i] = new Vector();
^
sc.java:35: v is already defined in start()
Vector v[i] = new Vector();
^
2 errors
What's wrong?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The problem is that v was already declared, so putting "Vector" in front of v a second time is incorrect. Replace
<pre> Vector v[i] = new Vector();</pre>
with
<pre> v[i] = new Vector();</pre>
[This message has been edited by Jim Yingst (edited August 22, 2001).]


"I'm not back." - Bill Harding, Twister
Dalibor Toth
Ranch Hand

Joined: Jul 16, 2001
Posts: 38
Thanks, enormously!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: creating variables in a loop