• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating multi object in a loop without using list

 
ching lam
Greenhorn
Posts: 4
Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I need some help.

Is it possible to create multiple objects in a while loop without using list/array?
Actually, I have not learned to use array...

Here is part of drive programme:

Car car1 = new Car("Toyota", "Camry");
Car car2 = new Car("Hyundai", "Santa Fe");
Car car3 = new Car("Ford", "Escape");

any hint on how I create the objects?

Thanks.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us the while loop and explain why the objects have to be created inside it?
 
ching lam
Greenhorn
Posts: 4
Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64961
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's your issue with using the right tool for the job?
 
ching lam
Greenhorn
Posts: 4
Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my problem is I dont know how to create an new object in each loop without using list.

I cant say Car car(i) = new Car() to create car1, car2, car3 and so on.

Furthermore, I will need to count how many cars I have created, does this imply that I must use a list?


Please help.

Or just tell me it is impossible without using list
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it's not impossible. I also recommend using the right tool for the job but will suggest an alternative.

You could add a counter to your do/while loop and increment it each time through the loop. You can then use the counter to create an object name, e.g.


then


setting the object's details in a constructor or using mutator methods as you suggest in your snippet.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64961
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Brannon wrote:



This will not even come close to working. Did you actually try it?

Again I ask, what is the point of the question? (I'll be seriously disappointed if this turns out to be one of those "stupid interview questions".)
 
ching lam
Greenhorn
Posts: 4
Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is just a school assignment. I learned java for 4 weeks only. hehe
We are in Beginning Java, right?


And I am given a drive programme

Car car1 = new Car("Toyota", "Camry");
Car car2 = new Car("Hyundai", "Santa Fe");
Car car3 = new Car("Ford", "Escape");
...
...
...
System.out.println("How many cars? " + Car.getCount());
...

In this case, I think I cant count objects in a loop. This make me believe that I should use a list (which I have not learned yet). **wired
And not just counting, I will need to do car2.setMake(make), car1.setModel(model). All in all, I have to able to access all objects.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic