简体   繁体   中英

Error “Can only iterate over an array or an instance of java.lang.Iterable” using iterator on a class that contains a private collection

i want to use an Iterator on a class that has a collection (defined by me as a dictionary) on which I implemented the Iterator. How can I do? Do I have to call back the private field of the class when I have to do a for-each loop? Here is the code of the collection class:

public class ArrayDict<K, V> implements Dictionary<K, V> {

Coppia<K, V>[] dict = new Coppia[1];
int n = 0;

@Override
public Iterator<K> iterator() {
    return new Iterator<K>() {

        private int i = 0;

        @Override
        public boolean hasNext() {
            return i < n;
        }

        @Override
        public K next() {
            int pos = i;
            i++;
            return(K) dict[pos].key;
        }

    };
}

@Override
public void insert(K key, V value) {

    if(search(key)==null)
        throw new EccezioneChiavePresente("Chiave già presente");

    dict[n] = new Coppia<K,V>(key, value);
    n++;
    if (n == dict.length) {
        Coppia<K,V>[] tmp = new Coppia[dict.length * 2];
        for (int i = 0; i < n; i++)
            tmp[i] = dict[i];
        dict = tmp;
    }
}

@Override
public void delete(K key) {
    if (n == 0)
        throw new EccezioneDizionarioVuoto("Dizionario vuoto");
    if (search(key) == null)
        throw new EccezioneChiaveNonPresente("Chiave non presente");
    int i;
    for (i = 0; i < n; i++)
        if (dict[i].key.equals(key))
            break;
    for (int j = i; j < n - 1; j++)
        dict[j] = dict[j + 1];
    n--;
    if (n > 0 && n < dict.length / 4) {
        Coppia<K,V>[] tmp = new Coppia[dict.length / 2];
        for (i = 0; i < n; i++)
            tmp[i] = dict[i];
        dict = tmp;
    }
}

@Override
public V search(K key) {
    if(n==0)
        throw new EccezioneDizionarioVuoto("Dizionario vuoto");
    for (int i = 0; i < n; i++)
        if (dict[i].key.equals(key))
            return dict[i].value;
    return null;
}

}

And here is a part of the class that use the collection as private field:

public abstract class BibliotecaAbs {

protected Dictionary<String, Record> volumi;

public boolean bibliotecaVuota() {
    try {
        volumi.delete("");
    } catch (EccezioneDizionarioVuoto e) {
        return true;
    }
    return false;
}

public void addVol(String posizione, Volume volume) {
    try {
        volumi.insert(posizione, new Record(volume, false));
    } catch (EccezioneChiavePresente e) {
        throw new EccezioneScaffaleOccupato();
    }
}

Naturally I have a derived class that makes BibliotecaAbs concrete. I want to do something like this:

for(Object s : b){
        String intpos = s.toString();

But it gaves me the error:

Can only iterate over an array or an instance of java.lang.Iterable

How can I do to solve this problem? Any help would be appreciated

implements Iterable<SomeType>添加到BibliotecaAbs的类声明中,并实现所需的方法public Iterator<SomeType> iterator()

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