3

I already have the following code

public class Qn3
{
    static BigDecimal[] accbal= new BigDecimal[20];
    private static Integer[] accnums = new Integer[5];

    public static void main(String[] args)
    {
         int count;
         accnums = {1,2} //i cant add this line of code as well, what is wrong?
         while(accnums.length < 5)
         {
              count = accnums.number_of_filled_up_indexes 
               //this is not actual code i know 
           //do this as the number of values in the array are less than 5
           break;
          }
           //do this as number of values in the array are more than 5
    }
}

I must use this code there is no changing this is a requirment, so please dont suggest to use arraylist and such (i am aware of other array types and methods)

The problem is that as I have already declared that accnums must contain only 5 values, that is predefined.

I am trying to perform a check whether the ones that are not null and if all are null. To do this I have tried this but this is giving me 5 p(pre-defined integer array value not what I want).

1
  • Sorry, misunderstood the question.. See MadProgrammer's answer - just use a for loop and keep your count in a variable that you can increment for each index that is not null. The count at the end will be the total number of non-null values. Commented Aug 27, 2012 at 3:13

3 Answers 3

4
public static void main(String[] args)
{
    int count = 0;
    accnums = new Integer[] {1,2,null,null,null};
    for (int index = 0; index < accnums.length; index++) 
    {
        if(accnums[index] != null)
        {
            count++;
        }
    }

    System.out.println("You have used " + count + " slots);

}
Sign up to request clarification or add additional context in comments.

5 Comments

accnums.length - Collections.frequency(Arrays.asList(accnums), null)
PS accnums should be new Integer[5] { 1, 2, null, null, null }
@veer firstly, weren't allowed to Lists :P, secondly <duh> - cheers
No problem dude! I figured we could use Arrays.asList, just not replace the type of accnums :-)
@veer that's just my take on the requirements, reality may be different ;)
2

Try this...

accnums[0] = new Integer(1);
accnums[1] = new Integer(2);

Both the below will work if done during Declaration and Initializing time of and array.

Integer[] arr = new Integer[]{1,2,3};
Integer[] arr = {1,2,3}

But when you just declare the array as

Integer[] arr = new Integer[3]; // Still array holds no Object Reference Variable

then later initialize it this way...

arr = new Integer{1,2,3,};  // At this time it hold the ORV

Array are always initialized whether used at class or method scope, so for an int array all the values will be set to default as 0, and for Integer it will be null, as its an Wrapper object.

Eg:

    Integer[] arr = new Integer[5];

    arr[0] = 1;
    arr[1] = 2;

    System.out.println(arr.length);

    for (Integer i : arr){

        if (i!=null){

            count++;

      }



    }

    System.out.println("Total Index with Non Null Count :"+count);

}

1 Comment

yes tks that adds the value to the Integer array. but still my main question which is to check how many are filled up in the Integer array. tks
0
accnums[0] = 1;
accnums[1] = 2;
final int count = accnums.length
    - Collections.frequency(Arrays.asList(accnums), null);
System.out.println("You have used " + count + " slots");

or, if you really must do it manually...

int count;
for (final Integer val : accnums) {
  if (val != null) {
    ++count;
  }
}

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.