1

I am relatively new to Java and I have taken some light courses on it. I am trying to emulate an exercise that I had a while back and I am having some trouble.

I have two classes. One is taking in data and the other is storing it.

public class Car{

public Car(String name, String color)
{
     this.name = name,
     this.color = color
}

How can I store this into the array (not an array list) that I created in this class:

public class CarDatabase {

Car[] carList = new Car[100];

public CarDatabase()
{
    // System.out.println("test");
}

public void createAccount(String name, String color)
{        
// this is where I am having trouble


    for (int i = 0; i < carList.length; i++)
    {

    System.out.println("Successfully created: " + name + 
            "." + "Color of car: " + color);
    break;


    }
}

I don't have a main method yet but I will need one later on to for example, PRINT out this array and that is what I can't wrap my head around - how do I store DATA/OBJECTS into the "CarDatabase" array so I can call methods with it later (instead of just being able to print it)?

Any help would be appreciated. Thanks!

4
  • 4
    carList[i] = new Car("lambo","RED"); Commented Nov 2, 2015 at 19:43
  • It's no different than any other array. Commented Nov 2, 2015 at 19:52
  • I don't quite understand your question...Are you having trouble with setting the size of the array? Like Car carList[] = new Car[???]; Commented Nov 2, 2015 at 19:57
  • i have googled a bit and ya, most people have told me to do what @sam mentioned - carList[i] = new Car("lambo","RED"); but the thing is: what if i don't want to instantiate the array manually and it needs to be input from the other class? i can't do something like carList[i] = new Car(); because there is a constructor on the previous method. Commented Nov 2, 2015 at 20:04

1 Answer 1

3

Not really sure what you are trying to achieve but I'll give it a go.

You could modify your CarDatabase class like so -

public class CarDatabase {

  Car[] carList = new Car[100];
  int carsStored;

  // No need for a constructor since we don't need any initialization.
  // The default constructor will do it's job.

  public void createAccount(String name, String color) {
    carList[carsStored++] = new Car(name, color);
  }
}

And your main method could look like -

public static void main(String[] args) {
  CarDatabase database = new CarDatabase();
  database.createAccount("Lambo", "Red");
  database.createAccount("Punto", "White");

  // To loop through your database, you can then do
  for(int i = 0; i < database.carList.length; i++) {
    Car car = database.carList[i];
    // Now you can call methods on car object.
  }
}

Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.