4

I want to know that how can i find the number of element in the normal array in java. For example if i have an int array with size 10 and i have inserted only 5 element. Now, i want to check the number of element in my array? Below is the code for more clarification. Please help

public static void main(String[] args) {

        int [] intArray = new int[10];
        char [] charArray = new char[10];

        int count = 0;

        for(int i=0; i<=5; i++) {

            intArray[i] = 5;    
            charArray[i] = 'a';


        }


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

        System.out.println("int Array element : "+ count);
    }
9
  • 2
    Just add count in original loop. Commented Feb 19, 2015 at 16:25
  • 1
    maybe it is time to learn Lists Commented Feb 19, 2015 at 16:27
  • @Vid - Just to be clear, you don't want to alter the array, you just want to get back "5 elements out of 10 are null" kind of thing, right? Commented Feb 19, 2015 at 16:31
  • @Hayden don't see that part. Suppose you already have that array with size = 10 and element = 5. Commented Feb 19, 2015 at 16:31
  • 1
    There is no such thing in Java as a "partially filled array." The entire array is filled; it's just that some elements may still be set to the default. Commented Feb 19, 2015 at 16:34

6 Answers 6

8

The code is wrong. You declare a int[] : this cannot contain null values so this statement will not compile :

if(intArray[j] != null) {

Then, if you want to store many items but you don't know howmany, you should use a Collection, for example ArrayList

List<Integer> ints = new ArrayList<Integer>(); //Or new ArrayList<>(); - Java7

Then, you can have the size with :

ints.size();

Now, if you really want to use an array, then you can for example count the number of non-zero values :

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

Or better in Java 7 :

for(int i : intArray) {
  if(i!=0) {
    count++;
  }
}

Even better in Java 8 :

Arrays.stream(intArray).filter(i -> i !=0).count();
Sign up to request clarification or add additional context in comments.

Comments

3

Check out Collection.frequency(). This will count how many times the specific Object (in this case null) appears in the Collection.

Below is just an example to help you along

String[] array = new String[5];

array[0] = "This";
array[1] = null;
array[2] = "is";
array[3] = null;
array[4] = "a test";

int count = Collections.frequency(Arrays.asList(array), null);

System.out.println("String: " + count + " items out of " + array.length + " are null");

int[] iArray = new int[3];
iArray[0] = 0;
iArray[1] = 1;
iArray[2] = 2;

List<Integer> iList = new ArrayList<>();

for (int i : iArray) {
    iList.add(i);
}

int iCount = Collections.frequency(iList, 0);

System.out.println("Int: " + iCount + " items out of " + iArray.length + " are zero");

char[] cArray = new char[3];
cArray[0] = 'c';
cArray[1] = ' ';
cArray[2] = 'a';

List<Character> cList = new ArrayList<>();

for (char c : cArray) {
    cList.add(c);
}

int cCount = Collections.frequency(cList, ' ');

System.out.println("Char: " + cCount + " items out of " + cArray.length + " are ' '");

Output:

String: 2 items out of 5 are null
Int: 1 items out of 3 are zero
Char: 1 items out of 3 are ' '

4 Comments

Working with string array but not with int or char.
Since int and char are primitive, you cannot check for null. What would be your criteria to check on those?
for int it can be 0 and for char ' ', but not working.
@Vid - Added the int and char example code for you as well. need to convert into a List first on those, since they are primitive
0

You could change your first loop to something like this:

for(int i=0; i<=5; i++) {
     intArray[i] = 5;    
     charArray[i] = 'a';
     count++;
}

Or better yet, instead of using an array use an ArrayList. Here is an example of how arraylists work.

ArrayList<Integer> intList = new ArrayList<Integer>();
ArrayList<Character> charList = new ArrayList<Character>();
for (int i = 0; i < 5; i++){
    intList.add(5);
    charList.add('a');
}
System.out.println("int list element : " + intList.size());

ArrayList's are dynamic and .size() will only return the size of items currently in the arraylist.

Comments

0

If you want to implement your own solution for counting elements in an array, you can always implement your own wrapper class with the array in it if you want to.

public class classWithArray {
    private int[] arr;
    private int count;        

    public classWithArray(int arrLength) {
        arr = new int[5];
        count = 0;
    }

    public void add(int num) {
        arr[count] = num;
        count++;
    }        

    public int getCount() {
        return count;
    }

    public int getElement(int index) {
        return arr[index];
    }

    // Add other wrapper methods.

}

You can make the array of a Generic type (I don't know much about Java for that though) to make it work for any data type.

Comments

0

Well, We can use count for non-zero elements in the array,The Code is Below::

public class SomeElementOfArrayUsingCount{

public static void main(String[] args){

     double total;//The sum of non-zero numbers in the Array.
     total=0;
     double average;//The average of non-zero numbers.
     int i;
     double count;//The number of non-zero numbers.
     count = 0;
     int[] list;
     list = new int[5];//make a container of 5.
     list[0]=2;
     list[1]=4;
     list[2]=4;
     list[3]=5;
     list[4]=2;

   for(i=0;i<list.length;i++){
       if(list[i]!=0){
           total = total + list[i];//Add element to the array
           count = count + 1;//and Count it
       }
   }

   if(count==0){
       System.out.println("There were no non-zero elements");
   }

   else {
       average = total/count;//Divide by number of items
       System.out.println("The average is " + average + " the total count is " + count);
   }
      } 
     }

Comments

-3

sysout(); This will help in your processes because it will print out the answer to what you are looking for.

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.