How to find element number in array in java?

I want to know how I can find the number of elements in a normal array in java. For example, if I have an int array with size 10, and I inserted only 5 elements. Now, I want to check the number of elements in my array? Below is the code for 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);
    }
+4
source share
5 answers

Check out Collection.frequency(). This will count how many times a particular object appears in the collection (in this case null).

Below is just an example to help you.

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 ' '");

Conclusion:

Line: 2 elements out of 5 - null Int: 1 element out of 3 is zero
Char: 1 element out of 3: ''

+3
source

. int[]: , :

if(intArray[j] != null) {

, , howmany, Collection, ArrayList

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

:

ints.size();

, , , , :

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

Java 7:

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

Java 8:

Arrays.stream(intArray).filter(i -> i !=0).count();
+5

- :

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

, ArrayList. , .

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 , .size() , arraylist.

0

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

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 a arraytype Generic(I don’t know much about Javafor this) to make it work for any data type.

0
source

Well, we can use count for non-zero elements in the array, the code 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);
   }
      } 
     }
0
source

All Articles