MissouriTiger
Programmer
I would be grateful for some advice. I'm looking for an elegant way to do this. I can make this work, but only in a sloppy (in my opinion) fashion.
Suppose I create an array as follows:
int [] PersonNum = new int[100];
...I have an array of length = 100, but for my program this is merely a maximim limit. It's the most the array will accept. In practice, however, the array will most likely hold less than 100 elements, so the rest of the array will be filled with '0' (the default value for int). But these '0' aren't data, so I don't want to access them later.
When I want to access the array, I only want the real values, not the extra '0', so how can I get the number of actual elements in the array, as opposed to the length, since the length is useless in this context? Is there a way to get this value, or must I keep track of the number of elements in advance, while the array is being filled?
(I hate to cycle through the entire array checking for values > 0. This seems wasteful, since the array might only have 20 elements.)
Final note: Since the actual values in this field are '1, 2, 3, 4, 5, etc. if I could determine the max value in the array, it would solve my problem.
Below is an example of something I would like to do (but the .length obviously will not work).
System.out.println
("Average Absolute Error = " + avgAbsError = avgAbsError/arrayPersonNum.length))
Sorry so long winded. Any suggestions will be much appreciated.
_______________________________________
Constructed from 100% recycled electrons
Suppose I create an array as follows:
int [] PersonNum = new int[100];
...I have an array of length = 100, but for my program this is merely a maximim limit. It's the most the array will accept. In practice, however, the array will most likely hold less than 100 elements, so the rest of the array will be filled with '0' (the default value for int). But these '0' aren't data, so I don't want to access them later.
When I want to access the array, I only want the real values, not the extra '0', so how can I get the number of actual elements in the array, as opposed to the length, since the length is useless in this context? Is there a way to get this value, or must I keep track of the number of elements in advance, while the array is being filled?
(I hate to cycle through the entire array checking for values > 0. This seems wasteful, since the array might only have 20 elements.)
Final note: Since the actual values in this field are '1, 2, 3, 4, 5, etc. if I could determine the max value in the array, it would solve my problem.
Below is an example of something I would like to do (but the .length obviously will not work).
System.out.println
("Average Absolute Error = " + avgAbsError = avgAbsError/arrayPersonNum.length))
Sorry so long winded. Any suggestions will be much appreciated.
_______________________________________
Constructed from 100% recycled electrons