Reputation: 3361
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)
Upvotes: 1
Views: 6219
Reputation: 39
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..
}
Upvotes: -1
Reputation: 2436
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.
Upvotes: 0
Reputation: 20859
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++){
Upvotes: 3
Reputation: 8374
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.
Upvotes: 2
Reputation: 5306
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++){
Upvotes: 1