When you do:
Destination [][] Deststotarray = new Destination[20][20];
You are actually acllocating the space for 20 pointers to arrays of Destinations which can hold 20 destinations effectivly, so .length with allways be the size of the array in memory, not the number of things in it.
There are several solutions to this:
Firstly you could make a method that you pass the array of Destinations into, and it goes through and counts now many non null items there are inside the array.
However if you are going to want to do this alot you might be better off making a class which has the array as a private member and some accessor functions to the array and keeps a count of the number of items so that it allways knows how many items there are in the array eg:
Code:
public class DestinationArray
{
private Destination[] dArray = new Destination[20];
private int nextLocation = 0;
public void addDestination(Destination d)
{
dArray[nextLocation++] = d;
}
public int getLength()
{
return nextLocation;
}
}
edit: Obviously this example code needs a bit more work before it is production quality :-)
Bookmarks