1

I have an integer array of size 50. So, as you know, initially the element values of this array will be all 0's. What I need to do is: If the array = [12,10,0,0,......0], the for loop needs to consider only the non-zero elements, so the loop should get executed only twice..once for 12 and next for 10. Similarly, if the array is = [0,0,0,......0], then for loop should not executed at all since all elements are zero. If the array is [12,10,5,0,17,8,0,0,.......,0] then the for loop should get executed for the first 6 elements, even though one of the inner elements is zero.

Here is what I have.This gives me an IndexOutOfBoundsException.Please help. Also, is there any way I can dynamically increase the size of an int array rather than setting the size to 50,and then use it in the loop? Thx in advacance for any help!

int[] myArray = new int[50];
int cntr = 0;
int x = getXvalue();
int y = getYvalue();

if (x>y){
   myArray[cntr] = x;
   cntr++;
     }

 for (int p=0; p<= myArray.length && myArray[p]!=0 ; p++){
//execute other methods..
}

//SOLUTION:

I've made use of an ArrayList instead of an int array to dynamically increase size of the array.

ArrayList<Integer> myArray = new ArrayList<Integer>();

To set the value of the elements-
myArray.add(cntr, x); // add(index location, value)
3
  • 2
    Consider using an ArrayList. It is simple to use and can resize dynamically. Commented Jan 30, 2012 at 6:33
  • Which line throws the exception? Commented Jan 30, 2012 at 6:37
  • @Arjan -The for loop throws an Exception Commented Jan 30, 2012 at 6:40

5 Answers 5

3

A cleaner way is:

The most convenient way to solve such things like this is using the appropriate Collection. For this example you might consider using an ArrayList<Integer>.

The advantage is that you do not have to preinitialize the list with a given size. It will also resize if necessary.

Another advantage is that you do not have to care about padding empty elements as well as handling empty elements at the end of the list.

The concrete problem is:

for (int p=0; p<= myArray[cntr].length && myArray[p]!=0 ; p++){
//execute other methods..
}

This should be illegal:

myArray[cntr].length

it needs to be myArray.length

also you iterate from 0..50 with <= this adresses 51 elements you do only have 50 elements so the correct line of code would be

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

1 Comment

Thx!I've used an ArrayList as per your suggestion.Solution included in my post. :)
2
for (int p=0; p<= myArray[cntr].length && myArray[p]!=0 ; p++){

This line shouldn't even compile. You probably meant myArray.length? The other mistake: use < not <=.

And you probably want a List here, not an array. Lists can be resized dynamically, arrays can't.

1 Comment

Yes..That was a typo...I meant myArray.length.
1

This is problematic:

for (int p=0; p<= myArray[cntr].length && myArray[p]!=0 ; p++){

myArray is an array of type int so you cannot use myArray[cntr].length. Do you perhaps mean this instead?:

for (int p=0; p<= cntr && myArray[p]!=0 ; p++){

Comments

0

This will help for your problem

int len = myArray.length;

    for (int p=0; p < len; p++){ 

        cntr++;
        while(myArray[p]==0){
            if((p+1) != len) p++;
        }
     System.out.println(myArray[p]);
     System.out.println("cntr: " + cntr++);
        //execute other methods.. 
    }

But a better way of handling arrays is using Collections ArrayList.

Comments

-1

Why dont you consider adding a continue statement inside the for loop itself like this

for (int p=0; p<= myArray.length ; p++)
{ 
     if( myArray[p]==0){
     continue;
     }
//execute other methods.. 
}

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.