1

I am declaring a String array as:

String[] items = new String[10];
items[0] = "item1";
items[1] = "item2";

How can I find length of items in an efficient way that it contains only 2 elements. items.length returns 10.

I am running a loop already which runs to its length. I want to so something with this code without adding new code/loop to count number of not-null elements. What can I replace with items.length

for (int i = 0; i < items.length; i++) {
...
}

7 Answers 7

8

No. You will need to loop and see how many non-null elements there are.

Consider using e.g. an ArrayList<String> instead of a raw array.

UPDATE

To answer the new part of your question, your loop can become:

for (int i = 0; (i < items.length) && (items[i] != null); i++) {
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

@imrantariq: Oli's answer is still relevant for the second part of your question.
2

Why not use a collection:

Vector <String> items;
items.add("item1");
items.add("item2");

int length = items.size();

Comments

1

It is 10 already it is just that 8 of the object are set to null so you could do following

int count = 0 ;
if(items!=null){
  for(String str : items){
    if(str != null){
      count ++;
    }
  }
}

1 Comment

no, there is no nulls. Arrey uses not cleared memory space. You need to set nulls there if u whish to find them there.
0

With the modified question in mind, you can absolutely do nothing. There is no attribute giving you the count of not null elements, so either you'd take another collection or you'd check each value for null.

Comments

0

I think may this will help u

public class T1 {
    public static void main(String[] args) {
        String[] items = new String[10];
        items[0] = "item1";
        items[1] = "item2";     
        System.out.println(getActualSize(items));

    }

    public static int getActualSize(String[] items)
    {
        int size=0;
        for(int i=0;i<items.length;i++)
        {

            if(items[i]!=null)
            {
                size=size+1;
            }
        }
        return size;

    }
}

Comments

0

You need to iterate and check for null.

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

count will give the number of occupied elements

1 Comment

@EI can you try String[] items = new String[10]; and print all the items in the array. You will see null. So why are you saying there is not null !
0

To find a number of all non-null elements in array/collection (that are not neccesary in the beginning of array) you can use elegant guava solution: Iterables.size(Iterables.filter(items, Predicates.notNull())).

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.