my dog learned polymorphism*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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: 557
Can you show us the while loop and explain why the objects have to be created inside it?


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
ching lam
Greenhorn

Joined: Jun 03, 2011
Posts: 4

yup
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

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: 557
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: 60046
    
  65

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
 
Similar Threads
Accessing Arrays and Editing Objects Within Them From Other Class'
variable in place of object?
Problem geeting value in JSP after type casting Gurus Please help me
java.lang.ArrayStoreException
Chreating an array of vectors