• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating multi object in a loop without using list

 
Greenhorn
Posts: 4
Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yup
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic