This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How do you create an array of objects.

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically I have created an object and this object contains all the attributes I need. How would I go about creating a array of say 10 of these objects.

Also, can you name an object via a variable like

Car myVariable
myVariable = new Car (millage, engineSize, mpg, status, manufacturer, registration);

Thanks...
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Boy,

You may not have noticed our naming policy when you signed on here at the Ranch; it basically requires that your display name be a real name. (You might not have noticed our "be nice" oolicy either, which would seem to preclude naughty words in display names.) Please head over here and update your display name ASAP. Thanks!
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can use an array, check out the api

api

You create an array of objects:
Car[] carArray = new Car[10];
Car car = new Car( <here come your params> );
carArray[1] = car;

usually you want a loop to fill the array, so you dont have to do them all by hand.
another idea would be to use ArrayList where you can use differnt index than just an int as in Arrays.

regarding your second question, if you have a constructor that takes the parameter, yes you can assign the object to the variable.

hope that helps,
ben
 
Mr Ben
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// Init carArry and set variables
Car [ ] carArray;
carArray = new Car [10];



//Possition In Array Manufact Model E.Size Reg Millage MPG Status
carArray[0] = new Car("Vauxhall", "Corsa", 1.3, "GP 03 CBT", 8123, 35, 'Y');
carArray[1] = new Car("Vauxhall", "Agila", 1.2, "AB 03 FTR", 10045, 38, 'S');
carArray[2] = new Car("Vauxhall", "Agila", 1.0, "FT 01 YUT", 21488, 40, 'N');
carArray[3] = new Car("Vauxhall", "Astra", 1.6, "RT 51 DRE", 15032, 32, 'S');
carArray[4] = new Car("Vauxhall", "Astra", 2.2, "DR 51 KLP", 19933, 25, 'Y');
carArray[5] = new Car("Ford", "Fiesta", 1.0, "GP 53 REW", 4987, 40, 'Y');
carArray[6] = new Car("Ford", "Fiesta", 1.3, "SR 01 BHG", 1997, 35, 'N');
carArray[7] = new Car("Ford", "Mondeo", 1.6, "WE 02 GTY", 17342, 33, 'N');
carArray[8] = new Car("Ford", "Mondeo", 2.0, "QW 52 EWQ", 12345, 25, 'Y');
carArray[9] = new Car("Ford", "Orion", 1.8, "SD 01 TYR", 24459, 28, 'N');

I currently have this but I keep on getting this error when I try and compile...

C:\Documents and Settings\Mr Ben\Desktop\Java 2\carHireCoursework.java:22: cannot resolve symbol
symbol : method Car (java.lang.String,java.lang.String,double,java.lang.String,int,int,char)
location: class carHireCoursework
carArray[1] = Car("Vauxhall", "Agila", 1.2, "AB 03 FTR", 10045, 38, 'S');
^

This error appears for each line when im putting an object in the array...
 
Mr Ben
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, wrong error////

C:\Documents and Settings\Mr Ben\Desktop\Java 2\carHireCoursework.java:23: cannot resolve symbol
symbol : constructor Car (java.lang.String,java.lang.String,double,java.lang.String,int,int,char)
location: class Car
carArray[2] = new Car("Vauxhall", "Agila", 1.0, "FT 01 YUT", 21488, 40, 'N');
 
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your Car class should have matching constrctor with same signature as the line on which you are creating a specific element in the array.
[ May 13, 2004: Message edited by: Kishore Dandu ]
 
Mr Ben
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// Atributes set here

private String manufacturer;
private String model;
private double engineSize;
private double millage;
private String registration;
private int mpg;
private char status;




//Constructor
public Car (String manufacturerIn, String modelIn, double engineSizeIn, double millageIn, String registrationIn, int mpgIn, char statusIn)

{
manufacturer = manufacturerIn;
model = modelIn;
engineSize = engineSizeIn;
millage = millageIn;
registration = registrationIn;
mpg = mpgIn;
status = statusIn;
}

Here is my constructor, what do you mean by signature???
[ May 13, 2004: Message edited by: Mr Ben ]
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fourth argument you are passing is a string. Where as in the constructor it is a double. I think that is why you are having compilation problems.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr Ben,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

We'd like something other than a title for the displayed first name.

Thanks Pardner! Hope to see you 'round the Ranch!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!