Is it possible to use a primitave value in a reference object's name?
I'm trying to set up a loop to read a list of double values from a file, and make an object for each one. Something like:
int objectnumber = 1;
Box box-objectnumber = new Box(br.readLine());
objectnumber++;
So that I get 40 Box objects named box1 through box40. Any ideas? If I need to, I can just write out a hundred lines of code, but that would look stupid. And yes, I'm a noob.
Name:
Anonymous2006-04-07 17:26
What is an array, my precious?
Name:
Anonymous2006-04-07 17:39
Box[] boxen = new Box[40];
for(int i = 0; i < 40; i++)
{
boxen[i] = new Box(br.readLine());
}
Name:
Anonymous2006-04-07 17:51
Would this work if I'm trying to move them from one stack to another? Im still kinda new to stacks, which is what the assignment is focusing on.
Name:
Anonymous2006-04-07 18:14
Comedy gold. Let me guess, you have to preserve the order of the objects when moving them between the stacks?
Name:
Anonymous2006-04-07 18:41
No, I have a robot that pops Boxes from a PickUpStation stack and moves it to one of 8 other stations' stacks. Order doesn't matter. I don't get it, why is it so funny? Is preserving stack order some cliche project or something? I didn't sleep last night, maybe I'm not making much sense?
Name:
Anonymous2006-04-07 19:07
Moving objects between stacks without reversing them is a good newbie exercise -- forces you to think like a programmer.
Anyway, I don't really understand what problem you have with your assignment. Are you supposed to create the stack class by yourself or do you already have one?
Name:
Anonymous2006-04-07 19:15
Both. I'm about to test my program. If it works, I'll post it on rapidshare or something when I get back so you can see what I'm talking about. If it doesn't work, I'm going to dinner with some friends and will work on it later tonight.
Name:
Anonymous2006-04-07 19:21
<code>Box[] boxen = new Box[40];
for(int i = 0; i < 40; i++)
{
boxen[39 - i] = new Box(br.readLine());
}</code>
Name:
Anonymous2006-04-07 19:22 (sage)
Box[] boxen = new Box[40];
for(int i = 0; i < 40; i++)
{
boxen[39 - i] = new Box(br.readLine());
}
Name:
Anonymous2006-04-07 21:24
Success! I got back from dinner, sorted out some typo-errors, ran it, and it works perfectly. I also now have a much better grasp on stacks, which makes the assignment a good one, I guess. And yeah, just using an array did the trick. Not sure why I thought that wouldn't work... Oh, well. Thanks guys! If anyone actually cares enough to see what the assignment was or what I did, just ask and I'll rapidshare it or something. Otherwise I won't bother.
Name:
Anonymous2006-04-08 5:09 (sage)
>>11
RapidShare would bo cool, I'd like to see the finished program if you don't mind
Name:
Anonymous2006-04-09 15:18
>>11
Also note that you can create an array this way too: Box boxen[] = {new Box(br.readLine(), new Box(br.readLine(),...new Box(br.readLine()};
>>12
Sorry it took so long to reply. Went to the beach for a week and then got swamped with all the stuff I missed. I know it sucks, but I did end up with an A for it, even though I only worked on it that one night.