Why Java Numbers Don't Repeat

I can't help but wonder why I can't write something like this:

for (int i : 3) { System.out.println(i); } 

for print:

 0 1 2 

I mean, 3 can be automatically placed in an Integer , which can be Iterable .
I know that I selected the first element 0 , but I assume that this is a common case and that it can facilitate the calculation using such a ForEach construct.

+7
source share
7 answers

This is stupid, but you can 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 are statically importing a method:

 import static your.package.IterUtil.iter; 

from this custom class:

 public class IterUtil { public static Iterable<Integer> iter(int to) { return new IntIterable(0, to); } public static Iterable<Integer> iter(int from, int to) { return new IntIterable(from, to); } private static class IntIterable implements Iterable<Integer> { private int start; private int end; private IntIterable(int start, int end) { this.start = start; this.end = end; } @Override public Iterator<Integer> iterator() { return new Iterator<Integer>() { private int actual = start; @Override public boolean hasNext() { return actual != end; } @Override public Integer next() { int value = actual; if (actual < end) { actual++; } else if (actual > end) { actual--; } return value; } @Override public void remove() { // do nothing } }; } } } 
+10
source

Because the number is not a range. Converting a number to a range is ambiguous. There is nothing to prevent you from writing a Range class that is iterable, e.g.

 public class IntegerRange implements Iterable<Integer> { private final int _fromInclusive; private final int _toExclusive; public IntegerRange(int fromInclusive, int toExclusive) { _fromInclusive = fromInclusive; _toExclusive = toExclusive; } public Iterator<Integer> iterator() { return new Iterator<Integer>() { int c = _fromInclusive; public boolean hasNext() { return c < _toExclusive; } public Integer next() { if (c >= _toExclusive) { throw new NoSuchElementException(); } return c++; } public void remove() { throw new UnsupportedOperationException(); } }; } } 

Please note that with this approach, you can easily add features such as indicating the increment, regardless of whether the range is included on both sides, etc.

+9
source

In the syntax for(x : c) c must be Iterable ; 3 no. You can do

  for (int i : new int[]{0,1,2}) { System.out.println(i); } 
+4
source

You iterate over collections of objects. and Integer is the only object from which nothing needs to be iterated over. However, you can simply do this:

 for(int i=0;i<number;i++) 
+4
source

In java, the foreach loop only iterates over arrays or collections that implement Iterable , which the Integer wrapper type does not.

But it makes sense when you think about it. What does β€œfor every integer I'm in 3, do it” mean? How many integers in 3? Will we start from zero, so there are 4 integers (0,1,2,3)? Are we starting at 1? Do we always step one?

If you want to emulate this behavior, TheOtherGuy's answer will work, but a for-loop direct loop is probably even better.

+4
source

As others have noted, Java for-each iterates over an array or collection. "3" is not an array or collection, it is one value. If Java allows the construct that you propose, the only sequential implementation will be "iterate" over a single value, 3. This is:

 for (int i : 3) { System.out.println(i); } 

outputs:

 3 

This is not very useful, so they probably did not implement it.

Your question assumes that a single value of type "3" implies a range. But why do 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 literally an infinite number of other options.

To make this specific, you need to specify a start value, end value and increment. Hmm, maybe we could come up with this entry:

 for (int i=0,3,1) 

But this is still ambiguous. It is unclear whether we want to continue to i == 3 or to the last value <3. And we probably need to distinguish the plus plus from the minus increment. So, how about this, more flexible notation:

 for (int i=0; i<=3; i=i+1) 

Oh wait, what is already supported .:-)

+2
source

A number has non-classable subclasses such as Float and BigDecimal, so it makes no sense to implement Iterable <Integer> on Number. If Integer and Long implemented Iterable <Integer|Long >, it would be nice.

+1
source

All Articles