Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By JosAH

Thread: How to create and store multiple instances of objects?

  1. #1
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default 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!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,365
    Blog Entries
    7
    Rep Power
    20

    Default 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:

    Java Code:
    List<Car> cars= new ArrayList<Car>();
    for (int i= 0; i < nofCarsWanted; i++)
       cars.add(new Car());
    kind regards,

    Jos
    Zigster likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default 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.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,365
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: How to create and store multiple instances of objects?

    How can I print out the topspeed of the Car objects then?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,365
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to create and store multiple instances of objects?

    Quote Originally Posted by Zigster View Post
    How can I print out the topspeed of the Car objects then?
    Simply print a Car object and its toString() method will do the rest; try it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default 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:
    Syntax error on token "Car", VariableDeclaratorId expected after this token
    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.

    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.

  8. #8
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: How to create and store multiple instances of objects?

    Java Code:
    int a=3;
    System.out.println(a);    // Prints out "3"
    Car chevy = new Car();
    System.out.println(chevy);    //Prints out chevy.toString();
    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?

  9. #9
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: How to create and store multiple instances of objects?

    Thanks Diargg, this is my Main class now:

    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);
    		}
    	}
    }
    And it prints:

    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

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: How to create and store multiple instances of objects?

    It seems wasteful to put something into an arraylist and immediately retrieve it:
    Java 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);
            }
    vs
    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

  1. Multiple object instances in array of objects
    By Drizzt in forum New To Java
    Replies: 6
    Last Post: 01-12-2012, 04:07 PM
  2. how to store the instances of this file
    By aneuryzma in forum New To Java
    Replies: 1
    Last Post: 03-27-2011, 01:42 PM
  3. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  4. Can I store multiple objects in an array
    By lareauk in forum New To Java
    Replies: 9
    Last Post: 05-29-2008, 03:57 AM
  5. Can I use vectors to store multiple types of objects
    By Nathand in forum Advanced Java
    Replies: 6
    Last Post: 04-28-2008, 07:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •