This is kind of stupid, but you could write something like this.
This is kind of stupid, but you could write something like this: for (int I : iter(3)) { System.out. Println(i); // 0, 1, 2 } for (int I : iter(-5)) { System.out. Println(i); // 0, -1, -2, -3, -4 } for (int I : iter(1, 7)) { System.out.
Println(i); // 1, 2, 3, 4, 5, 6 } if you would statically import method: import static your.package.IterUtil. Iter; from this custom class: public class IterUtil { public static Iterable iter(int to) { return new IntIterable(0, to); } public static Iterable iter(int from, int to) { return new IntIterable(from, to); } private static class IntIterable implements Iterable { private int start; private int end; private IntIterable(int start, int end) { this. Start = start; this.
End = end; } @Override public Iterator iterator() { return new Iterator() { private int actual = start; @Override public boolean hasNext() { return actual! = end; } @Override public Integer next() { int value = actual; if (actual end) { actual--; } return value; } @Override public void remove() { // do nothing } }; } } }.
1 not stupid at all. Using this in my tests now, and look how readable they are! 10X.
– Asaf May 2 '11 at 20:33 oh, and it's for (int I : range(1,3)) which produces 1,2,3 (inclusive) – Asaf May 2 '11 at 20:47.
Because a number is not a range. Converting a number to a range is ambiguous. There is nothing stopping you from writing a Range class that is iterable, for example public class IntegerRange implements Iterable { private final int _fromInclusive; private final int _toExclusive; public IntegerRange(int fromInclusive, int toExclusive) { _fromInclusive = fromInclusive; _toExclusive = toExclusive; } public Iterator iterator() { return new Iterator() { int c = _fromInclusive; public boolean hasNext() { return c = _toExclusive) { throw new NoSuchElementException(); } return c++; } public void remove() { throw new UnsupportedOperationException(); } }; } } Note that with such an approach, you can easily add features like specifying the increment, whether range is inclusive on both sides, etc.
1 for the correct answer to my question: ambiguity. Using Matej's solution thou. 10x for the code.
– Asaf May 2 '11 at 20:32.
You iterate over collections of Objects. And Integer is a single Object there is nothing to iterate over. However, you could just do this: for(int i=0;i.
1 Exactly what I was thinking. The only use a for(int I : 3) loop would have is saving the programmer 10 characters and make more work in the for() back end. – Shaded May 2 '11 at 15:34 1 @Shaded: 10 chars, but the code is cleaner.
– Asaf May 2 '11 at 20:39.
In java, the foreach loop only iterates over either arrays, or collections that implement the Iterable interface, which the wrapper type Integer does not. But it makes sense when you think about it. What does "for each integer I in 3, do this" mean?
How many integers are in 3? Do we start at zero, so there are 4 integers (0,1,2,3)? Do we start at 1?
Do we necessarily always step by one? If you want to emulate that behavior, TheOtherGuy's answer will work, but a straightforward for-loop is probably even better.
In for(x : c) syntax, c needs to be an Iterable; 3 is not. You can do for (int I : new int{0,1,2}) { System.out. Println(i); }.
Iterable, not Iterator – Sean Patrick Floyd May 2 '11 at 15:27 1 c needs to be an Iterable. – Andy Thomas-Cramer May 2 '11 at 15:28 not answering my question, and your for loop isn't better than the old C style. – Asaf May 2 '11 at 20:35.
As others have noted, Java's for-each iterates over an array or collection. "3" is not an array or collection, it's a single value. If Java allowed the construct you're suggesting, the only consistent implementation would be to "iterate" over the single value, 3.
That is: for (int I : 3) { System.out. Println(i); } would output: 3 Which doesn't seem very useful, which is probably why they didn't implement it. Your question supposes that a single value like "3" implies a range.
But why would you assume {0,1,2,3}? Why not {1,2,3}? Or {-3, -2, -1, 0, 1, 2, 3}?
Or {0.5, 1.0, 1.5, 2.0, 2.5, 3}? Or a literally infinite number of other possible variations. To make it concrete, you'd have to specify the starting value, the ending value, and the increment.
Hmm, maybe we could come up with a notation like this: for (int i=0,3,1) But that's still ambiguous. It's not clear if we want to continue until i==3, or until the last value So how about this, more flexible notation: for (int i=0; i.
Number has non Integer subclasses such as Float, and BigDecimal so it doesn't make sense to implement Iterable on Number. Having Integer and Long implement Iterable would be nice though.
1 didn't think about Numbers in that way. Of course I want only integers (mathematically speaking) – Asaf May 2 '11 at 20:36.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.