简体   繁体   中英

an error after compiling the code-cannot find symbol

i have this code:

static ArrayList<Integer> output_list = new ArrayList<Integer>();
static ArrayList<Integer> pair_list = new ArrayList<Integer>();

    for (Iterator it=output_list.iterator(); it.hasNext();)
        {
            int pair_test = (int)output_list.next();
            for (int i=0; i<pair_list.length; i++)
            {

                if(!pair_list[i]==0)
                {
                    if (pair_list[i]==pair_test[i])
                    {
                        boolean check;
                        check=true;
                    }
                }
            }
        }

but i got an error "cannot find symbol" while compiling.

symbol  : variable length
location: class java.util.ArrayList<java.lang.Integer>
                        for (int i=0; i<pair_list.length; i++)
                                                 ^
Prior.java:79: array required, but java.util.ArrayList<java.lang.Integer> found
                                if(!pair_list[i]==0)
                                             ^
Prior.java:79: incomparable types: boolean and int
                                if(!pair_list[i]==0)
                                                ^
Prior.java:81: array required, but java.util.ArrayList<java.lang.Integer> found
                                        if (pair_list[i]==pair_test[i])
                                                     ^
Prior.java:81: array required, but int found
                                        if (pair_list[i]==pair_test[i])
                                                                   ^

anyone can help me why i got this error?

Use pair_list.size() instead of pair_list.length . The pair_list is a List not an array. I suggest looking at the for-each loop syntax also. This pair_list[i] will not compile too. It is array syntax. Use pair_list.get(i) instead.

There's some work on this code

  • as zacheusz already stated, a List is not an array, I think it's better to stay with the List but that means that means that.length becomes .size() and [i] becomes .get(i)
  • you define int pair_test = (int)output_list.next(); yet you say pair_test[i] , somethings is wrong there...
  • if(!pair_list[i]==0) should become if(!(pair_list[i]==0))

Which gives us as a first iteration:

static List<Integer> output_list = new ArrayList<Integer>();
static List<Integer> pair_list = new ArrayList<Integer>();

for (Integer pair_test: output_list) {
    for (int i=0; i<pair_list.size(); i++) {
            if(!(pair_list.get(i)==0)) {
                // i suppose that this is what you mean 
                if (pair_list.get(i)==pair_test) { 
                    boolean check;
                    check=true;  
                    // this is pretty useless as it lives very locally and does 
                    // absolutely nothing anywhere else in the program....
                }
            }
        }
    }
}

Now, probably you can rewrite the inner loop like

for (Integer pair_list_element : pair_list) {
    if ((pair_list_element != 0) && (pair_list_element==pair_test)) {
        boolean check = true;
        // and then probably, because your condition was satisfied
        do_something_useful();
        // and get out
        break;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM