简体   繁体   中英

Use a for loop to iterate over a new Class in java

I am trying to get the hang of using the for loop with a new class I created a simple java program that'll print from one number all the way to the other. I can't seem to get the hang of the for loop Here is the sample code I have made to test it:

import java.util.Iterator;
public class Try {
   public static void main(String[] args) {
      for (int i : new IntegerLoop(1, 9)) {
        System.out.print(i + " ");
      }

      System.out.println();

      for (int i : new IntegerLoop(-3, 3)) {
        System.out.print(i + " ");
      }

      System.out.println();

   }
}

This is the IntergerLoop class that just handles the first and last number to print out import java.util.Iterator;

public class IntegerLoop implements Iterable<Integer>, Iterator<Integer> {
    int first, last;
    int nextInt;

    public IntegerLoop(int f, int l) {
        first = f;
        last = l;
        nextInt = first;
    }

    public boolean hasNext() {
        return nextInt >= first && nextInt <= last;
    }

    public Integer next() {
        if (hasNext()) {
            int result = nextInt; // needs Iterator
            nextInt++;
            return result;
        }
        return null;
    }

    @Override
    public void remove() {
        // TODO Auto-generated method stub

    }

    @Override
    public Iterator<Integer> iterator() {
        // TODO Auto-generated method stub
        return null;
    }
}

You're getting a NullPointerException because you are returning null from the iterator method...

@Override
public Iterator<Integer> iterator() {
    // TODO Auto-generated method stub
    return null;
}

The iterator method should return a new instance of an iterator each time it is called.

You could use a inner class of Iterator , but for this example, I separated it, as this makes the IntegerIterator more easily re-usable (IMHO)...

IntegerLoop class...

public class IntegerLoop implements Iterable<Integer> {

    int first, last;

    public IntegerLoop(int f, int l) {
        first = f;
        last = l;
    }

    @Override
    public Iterator<Integer> iterator() {
        return new IntegerIterator(first, last);
    }
}

IntegerIterator class...

public class IntegerIterator implements Iterator<Integer> {

    int first, last;
    int nextInt;

    public IntegerIterator(int first, int last) {
        this.first = first;
        this.last = last;
        this.nextInt = first;
    }

    @Override
    public boolean hasNext() {
        return nextInt >= first && nextInt <= last;
    }

    @Override
    public Integer next() {
        if (hasNext()) {
            int result = nextInt; // needs Iterator
            nextInt++;
            return result;
        }
        return null;
    }

    @Override
    public void remove() {
        // TODO Auto-generated method stub

    }

}

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