Priyatam Roy
Priyatam Roy

Reputation: 83

Display 1 to 100 without loop or recursion

I found the solution to this problem but can't figure out how it works. Can someone please explain this?

public static void main(String[] args) {
Object[] numbers = new Object[100];
Arrays.fill(numbers, new Object() {
    private int count = 0;
    @Override
    public String toString() {
        return Integer.toString(++count);
    }
});
System.out.println(Arrays.toString(numbers));
}

[I could not comment to that answer directly because I dont have enough reputation points.]

Upvotes: 4

Views: 755

Answers (5)

Svetlin Zarev
Svetlin Zarev

Reputation: 15723

Here is a better solution:

IntStream.rangeClosed(0, 100).forEach(System.out::println);

I think it's self explanatory

Upvotes: 1

andy
andy

Reputation: 1356

What about this:

public class NoLoopPrint {

    int max;
    int from;

    public NoLoopPrint(int max) {
        this(1, max);
    }

    public NoLoopPrint(int from, int max) {
        this.max = max;
        this.from = from;
    }

    public String toString() {
        if (from >= max) {
            return String.valueOf(max);
        }
        System.out.println(from++);
        return this.toString();
    }

    public static void main(String args[]) {
        System.out.println(new NoLoopPrint(100));
        System.out.println(new NoLoopPrint(10, 20));
    }
}

Upvotes: 0

dingalapadum
dingalapadum

Reputation: 2185

System.out.println(1)
System.out.println(2)
System.out.println(3)
System.out.println(4)
System.out.println(5)
System.out.println(6)
System.out.println(7)
...
System.out.println(100)

:D

To avoid the code-generator proposed by Dmitry, this can be easily done with just copy-paste

int i=0;
System.out.println(++i);
System.out.println(++i);
System.out.println(++i);
System.out.println(++i);
System.out.println(++i);
...
System.out.println(++i);
  • Write the System.out.println(++i) once.
  • Copy and paste 4 times.
  • Then you pick those 5 lines and copy paste them again.
  • You should now have 10 times that line. Select those 10 lines and copy paste another 9 times.
  • done!

Upvotes: 2

Force0234
Force0234

Reputation: 243

To be accurate, Arrays.fill() use a loop in its own implementation. In general, Arrays.fill fills an array by assigning the second parameter to every element of the first parameter (your array).

Your example has an Array of type Object and a length of 100 elements. In Arrays.fill(...) you generate a so-called anonymous class of type Object, which reimplements the toString-method by increasing the value of a counter (int count) and printing it after that.

Now, by calling Arrays.toString the toString() method of every element inside the array is executed (which is the same instance of the anonymous class here), resulting in printing the numbers from 1-100

Upvotes: 3

user1717259
user1717259

Reputation: 2883

Can someone please explain this?

This code is a use of

Arrays.fill(Object[] a, Object val)

Where the Object val provided is an anonymous class with its toString() method overridden.

Arrays.toString(numbers) calls the toString() on the anonymous class for each element in the a array. As the toString() is overridden to increment the count, you get incremented values each time it is called.

However, as @Eran has pointed out, it is not without a loop.

Upvotes: 4

Related Questions