0

So two days ago I asked for some help, and got some good stuff... Searching arrays for a value at specific indices

Now I've tried to get as far as I could on my own, and I am stuck with an error of "The type of the expression must be an array type but it resolved to int" at if(lLungArray[i]k] == 1)

int[] lLungArray = {1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1};
int lLung;
private void checkHealth() {
    // TODO Auto-generated method stub
    if(MainActivity.panel1 == 0){
        if(MainActivity.panel2 == 0){
            int[] secondArray = {0,1,8,9};
            int lLungCount = 0;
            for(int i=0;i<lLungArray.length;i++){
                for(int k:secondArray){
                    if(lLungArray[i][k] == 1) //Error here {
                        lLungCount++;
                    }
                }
            }
        }
}

My project is almost done, but I can't figure out how to resolve this error. If you read my first question, you'll see I am a novice and google hasn't been much help.

Community
  • 1
  • 1
  • I believe you come with this code by refering my answer from your previous question. you need 2 dimensional array for this. – Baby Mar 04 '14 at 11:41

3 Answers3

0

lLungArray is an array of int but here lLungArray[i][k] you're trying to provide 2 indexes which treats it as an array of arrays and that is plain wrong!

Update: Based on your comment,

I'd like to know if lLungArray has a 1 in positions 0,1,8,9 and how many instances the 1 appears. Using the above lLungCount should end up at 4. Is that what if(lLungArray[i]==k){ will help me do?

I guess this will help you achieve what you want.

private void checkHealth() {
    int[] secondArray = { 0, 1, 8, 9 };
    int lLungCount = 0;
    for (int k : secondArray) {
            // If the lLungArray elements at the positions specified by the 
            // elements of secondArray is 1 then increment the lLungCount
        if (lLungArray[k] == 1) {
            lLungCount++;
        }
    }
}
Rahul
  • 44,383
  • 11
  • 84
  • 103
  • @RJ I'd like to know if `lLungArray` has a 1 in positions `0,1,8,9` and how many instances the 1 appears. Using the above `lLungCount` should end up at 4. Is that what `if(lLungArray[i]==k){` will help me do? – William From Detroit Feb 27 '14 at 10:11
  • It seems like `if(lLungArray[1]==k){` would search the `lLungArray` for a 0, 1, 8 or 9, instead of searching for a 1 at positions 0, 1, 8, and 9. Again, I'm a novice so I'm just trying to put it into words and I'm probably wrong. – William From Detroit Feb 27 '14 at 10:16
  • @user2329162 - No it wouldn't do that. Actually I just guessed that without knowing the actual requirement. I updated the answer to help you fix that problem. Hope it helps :) – Rahul Feb 27 '14 at 10:19
0

you declared an one dimensional array:

int[] lLungArray = {1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1};

but you try to access it like a two dimensional array

if(lLungArray[i][k] == 1)

thats not possible

I think you want something like this:

secondArray[k] == 1
nano_nano
  • 12,351
  • 8
  • 55
  • 83
0

lLungArray[i][k] means ith row and kth column (2-Dimensional array). But your array is one dimensional.

sjngm
  • 12,423
  • 14
  • 84
  • 114
TheLostMind
  • 35,966
  • 12
  • 68
  • 104