Reputation: 5296
I'm working with object arrays in Java and it's just difficult for me to understand them, I guess it's just because I'm not use to them yet..
Basically what I'm trying to do is have an option menu come up.. when the user presses "1" (in the main application class) it brings them to another class (addVehicle()) where a Vehicle object is passed in as a parameter. (This all works fine)
At the top of this class I am limiting the vehicles to a maximum of 4:
Vehicle[] vehicles = new Vehicle[4];
And then here is the method that adds the vehicle:
public void addVehicle(Vehicle vehicle[]) throws FileNotFoundException
{
reader = new Scanner(file);
if(canAddVehicle(vehicles[vehicles.length - 1]))
{
vehicle[vehicles.length - 1] = new Vehicle();
vehicle[vehicles.length - 1].readRecord(reader);
System.out.println("Vehicle Added!");
}
else
{
System.out.println("You can not add more than 4 vehicles.");
}
}
I would like it to store this vehicle object in the next spot in the array (if there is still a spot open) as well as all of the information that goes along with that Vehicle
Am I doing this the right way? Is this vehicle object correctly storing into the array? Should I be asking for information about the vehicle in my first class?
Upvotes: 0
Views: 1188
Reputation: 5296
Found my solution! Thanks for the help
for(int i = 0; i < vehicles.length || !found; i++)
{
if(vehicles[i] == null)
{
Scanner reader = new Scanner(file);
vehicle[i] = new Vehicle();
vehicle[i].readRecord(reader);
found = true;
reader.close();
}
}
Upvotes: 0
Reputation: 431
If I understand correctly you are trying to use vehicles.length to count the amount of items already added. That's wrong. vehicles.length will always return 4. This a method to check the next available item (assuming that you still want to use arrays)
private <T> int nextAvailableIndex(T[] array) {
for (int i = 0; i < array.length; i++) {
if (array[i] == null) {
return i;
}
}
return -1;
}
You can use an ArrayList as suggested before. But, remember that ArrayList can grow indefinitely. Your code should look like:
List<Vehicle> l = new ArrayList<Vehicle>(4); // Initial/expected Capacity!
if(l.size() < 4) {
// do stuff
}
Upvotes: 0
Reputation: 76918
I think the problem is you're not understanding what vehicles.length
represents.
If you say:
Vehicle[] vehicles = new Vehicle[4];
then vehicles.length
will always be 4
. That's the length of the array; It's fixed (you can't resize an array) and it doesn't matter that you haven't assigned anything to any of its elements yet. You will have four elements when you create the array, each containing null
.
To do what you're trying to do using arrays, you have a couple options.
Vehicle
to, starting with 0
null
. (Note this is a very inefficient way to do this) The easier path would be to use one of Java's Collections that are dynamic and increase in size as you add things. An ArrayList for example.
Upvotes: 2
Reputation: 9642
Nope. As you have specified the length of the vehicles
array, the length will always return 4, so you'll always insert into the end of the array. Ideally, you need to keep a counter of the number of inserted elements if you want to do it this way (and check there's space!). Also, I don't know what your vehicle
parameter is doing, as it doesn't seem to be used.
A better way to do this would be to use an ArrayList
, although of course, that won't help you if you want to learn how to use Object arrays.
Upvotes: 1