Results 1 to 10 of 10
- 05-21-2012, 04:47 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
How to create and store multiple instances of objects?
What I am trying to do, is to create several hundred instances of an object that return random int values.
For instance, say I want to create a car store, and have 50 cars available to buy, with different stats for things like, top speed, price, fuel consumption, etc, for each car.
I can create a class for a car that returns random numbers, but how do I create multiple instances of a car all at once and store the info so they can be referenced? I know you can instantiate a class with "new", but how do I do that several hundred times at once? Do I use a loop? And how can I store the info, in an array?
I'm having trouble finding any examples or tutorials of what I want to do and would really appreciate some guidance.
Cheers!
- 05-21-2012, 04:50 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Re: How to create and store multiple instances of objects?
Suppose a Car object takes care of its own random values, a simple loop can do the job:
kind regards,Java Code:List<Car> cars= new ArrayList<Car>(); for (int i= 0; i < nofCarsWanted; i++) cars.add(new Car());
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-21-2012, 05:40 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
Re: How to create and store multiple instances of objects?
Thanks JosAH! That's exactly what I wanted, it's much easier than I thought it would be.
Edit: Actually I just have a question about something in my code:
Java Code:package Main; import java.util.Random; public class Car { Random generator = new Random(); int topspeed = generator.nextInt(10) + 1;{ //What are these parenthesise for? System.out.println(toString()); } //If I don't have them I get an error? public String toString(){ return "Top Speed: " + topspeed; } }Last edited by Zigster; 05-21-2012 at 05:50 PM.
- 05-21-2012, 06:03 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Re: How to create and store multiple instances of objects?
Those curly brackets embrace an initialization block (a simple System.out.println( ... ) statement here); personally I find the placement of those brackets obstrusive and above all: you don't need that System.out.println( ... ) statement; I'd remove everything between those brackets including the brackets.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-21-2012, 06:10 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
Re: How to create and store multiple instances of objects?
How can I print out the topspeed of the Car objects then?
- 05-21-2012, 06:28 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 05-21-2012, 06:47 PM #7
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
Re: How to create and store multiple instances of objects?
When do I print the Car object? It gives me an error if I try to use: System.out.println(Car); either in my Main class or my Car class. It gives me this error:
I tried: System.out.println(cars) in my Main class and it prints out the Array list, it prints it out like this: [Top Speed: 5, Top Speed: 7, Top Speed: 5, Top Speed: 8] (of course it's longer but just an example) and I can't seem to format it with + "\n" or anything. I want to print out each Car on a new line though.Syntax error on token "Car", VariableDeclaratorId expected after this token
Edit: Ok, not sure what I was doing before but \n does work, but not sure how to get ride of the [], and would still like to know how to print an object with a toString statement.Last edited by Zigster; 05-21-2012 at 07:30 PM.
- 05-21-2012, 07:42 PM #8
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
Re: How to create and store multiple instances of objects?
You've tried to print out the Array, not the Cars inside of it. Perhaps there's a way of accessing individual members of an Array?Java Code:int a=3; System.out.println(a); // Prints out "3" Car chevy = new Car(); System.out.println(chevy); //Prints out chevy.toString();
- 05-21-2012, 08:44 PM #9
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
Re: How to create and store multiple instances of objects?
Thanks Diargg, this is my Main class now:
And it prints:Java Code:package Main; import java.util.ArrayList; import java.util.List; public class Main { public static void main (String[] args){ List<Car> cars= new ArrayList<Car>(); for (int i= 0; i < 50; i++){ cars.add(new Car()); Car item = cars.get(i); System.out.println("Model Number: " + (i+1) + " \t" + item); } } }
Model Number: 1 Top Speed: 10 Accelerations: 1 Braking: 8 Handling: 2 Price: 14
Model Number: 2 Top Speed: 8 Accelerations: 8 Braking: 10 Handling: 6 Price: 21
Model Number: 3 Top Speed: 9 Accelerations: 9 Braking: 10 Handling: 5 Price: 25
Model Number: 4 Top Speed: 9 Accelerations: 7 Braking: 9 Handling: 8 Price: 19
- 05-21-2012, 08:52 PM #10
Re: How to create and store multiple instances of objects?
It seems wasteful to put something into an arraylist and immediately retrieve it:
vsJava Code:for (int i= 0; i < 50; i++){ cars.add(new Car()); Car item = cars.get(i); System.out.println("Model Number: " + (i+1) + " \t" + item); }
Java Code:for (int i= 0; i < 50; i++){ Car item = new Car(); // create a Car object cars.add(item); // add to list System.out.println("Model Number: " + (i+1) + " \t" + item); // print it }If you don't understand my response, don't ignore it, ask a question.
Similar Threads
-
Multiple object instances in array of objects
By Drizzt in forum New To JavaReplies: 6Last Post: 01-12-2012, 04:07 PM -
how to store the instances of this file
By aneuryzma in forum New To JavaReplies: 1Last Post: 03-27-2011, 01:42 PM -
read txt file,with some records, create objects and store objects in tables of a db.
By stamv in forum JDBCReplies: 1Last Post: 01-22-2009, 04:25 PM -
Can I store multiple objects in an array
By lareauk in forum New To JavaReplies: 9Last Post: 05-29-2008, 03:57 AM -
Can I use vectors to store multiple types of objects
By Nathand in forum Advanced JavaReplies: 6Last Post: 04-28-2008, 07:55 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks