Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get number of elements in array, when array not filled? 3

Status
Not open for further replies.

MissouriTiger

Programmer
Oct 10, 2000
185
US
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
 
Hi MissouriTiger,

you could use one of the collection classes contained in the java.util package. Using a Vector for example, allows you to add elements until you're done, and then you can ask it's size by calling it's 'size()' method. All collection classes work with Objects, so you have to put the ints into Integer wrappers.

An example:
Code:
import java.util.*;

public class VectorTest {
  public static void main(String[] args){
    Vector v = new Vector();
    v.addElement(new Integer(3));
    v.addElement(new Integer(6));
    System.out.println("Size of Vector v: " + v.size());
  }
}

note: to get the value of an Integer in the Vector, use:
Code:
Integer i1 = (Integer)(v.elementAt(index));
int i = i1.intValue();

For more information check out the Vector class in the Java API: regards,
Blaxo
 
Rather than a Vector I would recommend using an ArrayList, but in a similar manner

You can create it to a specified size (eg. 100) and use it's size() method to return the number of elements with in the array.
Eg. create an ArrayList 100 long, stick 14 integer in the array a call to .size() will return 14.

Code:
ArrayList aList = new ArrayList(100);
this ensures the ArrayList can hold upto 100 elements.

Code:
int numElements = aList.size();
this will return the number of elements in aList NOT the length of aList, which we know is 100.

Just a thought. ----------------------------------------
There are no onions, only magic
----------------------------------------
 
Thanks for the advice. I'm going to switch to a Collection. I should have done that in the first place, but I was too lazy to do the research.

I have another question: Which Collection Class would you recommend if I need 2 data fields (pairs of numeric data)? In the meantime I'll hit the books and read up on them.

Thanks again. You guys have been a big help (as always)! _______________________________________
Constructed from 100% recycled electrons
 
PS- I should probably add that what I need to do with the container is to:

1. Restrict it to a maximum of 100 elements.

2. Don't need to access individual elements, just need to pull them out one after another to display in a table, and at the same time add them in an accumulator for totals.

3. I won't be storing them permanently; only till the program ends.

This might help someone in recommending a suitable Collection Class.

_______________________________________
Constructed from 100% recycled electrons
 
Hmmm, if you have two data elements, you should probably encapsulate them in an obect and store the objects in the array/Collection.

The problem with the Collection idea is that there is no maximum limit to the size of the collection--by design.

There are several ways to do what you want to do.

1) Create your own 'datastructure' with the array as a backing store and keep a count of the number of elements as they are added and removed.

2) Use a Collection (e.g. ArrayList) but check the size before adding elements and throw an exception if someone tries to insert more elements than the max size.

I would choose option 1 and maybe even a combination of 1 and 2 (creating my own class but delegating to the composite List instead of an array).
 
For pairs of VALUES, use either:

[1] two private ArrayList members, and store your values using something like:
Code:
public class Store {
  private ArrayList _x = new ArrayList();
  private ArrayList _y = new ArrayList();
  public void add(int x, int y) {
    _x.add(new Integer(x));
    _y.add(new Integer(y));
  }
  ... other methods ...
}
[2] one private ArrayList member, and a custom Pair class,. like:
Code:
class Pair {
  private int x, y;
  public Pair(int x, int y) {
    this.x = x;
    this.y = y;
  }
  public int getX() { return x; }
  public int getY() { return y; }
}
public class Store {
  private ArrayList pairs = new ArrayList();
  public void add(Pair pair) {
    pairs.add(pair);
  }
  ... more methods ...
}
For KEY/VALUE pairs, you should use an implementation of Map or Set.
Cheers, Neil :)
 
Thanks a million. I really appreciate all the help. I've got some thinking to do. Hmmm... _______________________________________
Constructed from 100% recycled electrons
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top