0

say i have a class type and I have to declare an array to store this type. E.g.:

MyType[] t = new MyType[5]

This means I allocate 5 cells to store items of MyType. And inside my program let's say I added 2 MyType items (dynamically)

t[0] = new MyType(..);
t[1] = new MyType(..);

If i want to find out how many items are actually filled,

int count=0;
for(int j =0; j<t.length ; j++){
   if ( t[j] != null ){
      ++count;
   }
}

Is testing for null the correct way ? as i couldn't seem to increment count. count is always 0. Also, length() shows the size of the array which is 5. Is there some method to show the count of actual filled items? thanks

13
  • 1
    Please post a complete example that reproduces this. Commented Dec 21, 2013 at 15:55
  • The code you've given won't even compile, because you've got length() instead of length. Commented Dec 21, 2013 at 15:57
  • 1
    Yes, but if that's a typo, then what else might you have changed from your real code? Basically, we can't trust that the code you've posted is the code that is causing a problem, which makes it relatively pointless. As Sotirios says, please post a short but complete program demonstrating the problem. Commented Dec 21, 2013 at 15:59
  • 1
    @dorothy: make a simple, short, self-contained program that reproduces this. You'll see that the count will be incremented, and that the problem in your real code is thus elsewhere. Commented Dec 21, 2013 at 16:07
  • 1
    Well your full code may be quite long, but you should try to isolate the problem. The code you've got it fine apart from the typo, so if it's not working then something else must be wrong - and we can't tell without a way of reproducing the problem. Commented Dec 21, 2013 at 16:07

2 Answers 2

1

First, there is no .length() method on arrays. It's a property that you access as

for(int j =0; j<t.length ; j++){
   if ( t[j] != null ){
      ++count;
   }
}

As for whether this is the only way, I think it is. You can maybe find utilities like the Apache Commons Collections, but they will still ultimately do the same thing: iterate over the array and check each item.

This is because the term filled you use really has nothing to do with the array, rather, it just changes the nth element from being a reference to null to being a reference to something else. In other worse, an array with 5 null elements and an array with 5 non-null elements are both length 5.

If this is a way to get a variable-size array where length is how many things you have added, then you may want to consider ArrayList.

http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

You can use it as such:

List<MyType> t = new ArrayList<MyType>(5);

t.add(new MyType(..));
t.add(new MyType(..));

int count = t.size();  // returns 2
Sign up to request clarification or add additional context in comments.

1 Comment

thanks. that's what i needed, but how to do it on primitive arrays without any collections? thanks
1
Below code returns the filled count. 

package com.type;

public class MyType {
    private int id;
    private String name;

    public MyType(int id,String name){
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }


    public static void main(String a[]){
        MyType[] t = new MyType[5];
        t[0] = new MyType(1,"Check1");
        t[1] = new MyType(2,"Check2");

        int count=0;
        for(int j =0; j<t.length ; j++){
           if ( t[j] != null ){
              ++count;
           }
        }       
        System.out.println(">>"+count); // Count is 2
        System.out.println(">>"+t.length); // Length is 5   
    }   
}

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.