简体   繁体   中英

Java - getting “cannot find symbol” error when using hasNext() method

I encountered this error while using the haseNext() method for ArrayLists:

error: cannot find symbol
while(isduplicate == false && birthdays.hasNext())

this is my code:

import java.util.*;


class hello  
{  
public static void main(String args[])
{
    Integer size = 4;
    Integer count = 5;
    Integer doubleinarray = 0;

    for(Integer i = 0 ; i < count ; i++) {
        List<Integer> birthdays = new ArrayList<Integer>();
        birthdays = CreateSimulator(size);
        Integer countdown = size;
        boolean isduplicate = false;

        while(isduplicate == false && birthdays.hasNext()) {
            Integer date = birthdays.get(0);
            birthdays.remove(0);
            if(birthdays.contains(date)) {
                isduplicate = true;
                doubleinarray ++;
            }
        }
    }
    System.out.println(doubleinarray / count * 100);
}

public static List<Integer> CreateSimulator(int size)
{
    List<Integer> Birthdays = new ArrayList<Integer>(size);
    Random rand = new Random();

    for(Integer i =0 ; i < size ; i++) {
        Birthdays.add(rand.nextInt(364) + 1);
    }
    return Birthdays;
}
}    

I didn't understand why it doesn't accept the hasNext. besides this, the rest of the code works fine.

appreciate your help

thanks :)

you have to do something like:

Iterator<Integer> birthdaysIterator = birthdays.iterator();

And with the birthDaysIterator you can call hasNext .

But this is not recommended nowadays. You are better of performing a normal for, like:

with normal for:

for (int i = 0; i < birthdays.size(); i++) {
    System.out.println(birthdays.get(i));
}

with for-each loop:

for (Integer birthday : birthdays) {
   System.out.println(birthday);
}

with Java 8 streams:

birthdays.forEach((birthday) -> {
            System.out.println(birthday);
});

EDIT:

Per @OHGODSPIDERS, if you use the other 3 versions that I suggested, you will run into ConcurrentModificationException . In order to avoid that, you can either stick with your iterator, or you can use an intermediate list to keep the elements which you want to delete, and remove them all afterwards.

Example:

List<String> toRemove = new ArrayList<>();
for (String birthday : birthdays) {
    if (someCondition) {
        toRemove.add(birthday);
    }
}
birthdays.removeAll(toRemove);

birthdays is of type List which does not have a method of that name. What you are looking for is the iterator, which you can access like this: Iterator<Integer> iterator = birthdays.iterator() And use it to traverse the list. hasNext is a method of the type Iterator.

As mentioned, the List class does not have a hasNext() method

An alternate way to use this, would be to check if it is not empty

while (isduplicate == false && !birthdays.isEmpty()) {

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