wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Creating multi object in a loop without using list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating multi object in a loop without using list" Watch "Creating multi object in a loop without using list" New topic
Author

Creating multi object in a loop without using list

ching lam
Greenhorn

Joined: Jun 03, 2011
Posts: 4

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

Joined: Oct 24, 2010
Posts: 563
Can you show us the while loop and explain why the objects have to be created inside it?


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
ching lam
Greenhorn

Joined: Jun 03, 2011
Posts: 4

yup
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

What's your issue with using the right tool for the job?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
ching lam
Greenhorn

Joined: Jun 03, 2011
Posts: 4

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

Joined: Oct 24, 2010
Posts: 563
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

Joined: Jan 10, 2002
Posts: 61654
    
  67

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

Joined: Jun 03, 2011
Posts: 4

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating multi object in a loop without using list