0

I am a newbie and I am to fulfill an exercise which is to write a simple program which would produce an array in console:

0,

0, 1,

0, 1, 2,

I failed at google searching similar problems which would direct me at a solution.

I will greatly appreciate your help. This is what i have been trying to build upon, but I am completely stuck:


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] table = new int[11];
        for ( int i = 0; i <=10; i++){
            table[i] = i;
            System.out.println(i);
        }
    }


1
  • "produce an array in console" - note that the console is just a text output, i.e. there is no such thing as an array there. To produce output as wanted you could use 2 loops, one to iterate from 0 to your maximum value and inside that one that loops from 0 to the current value of the outer loop (e.g. for(int i = 0; i <= 10; i++ ) { for( int j = 0; j < i; j++) { /*print the number here*/ } /*print a linebreak here*/ }) Commented Oct 10, 2019 at 15:50

3 Answers 3

1

You can try streams:

import java.util.stream.Collectors;
import java.util.stream.IntStream;

IntStream.range(0, 15).forEach(
        x -> System.out.println(
                IntStream.rangeClosed(0, x)
                         .mapToObj(String::valueOf)
                         .collect(Collectors.joining(", ")))
);

Output:

0
0, 1
0, 1, 2
0, 1, 2, 3
0, 1, 2, 3, 4
0, 1, 2, 3, 4, 5
0, 1, 2, 3, 4, 5, 6
0, 1, 2, 3, 4, 5, 6, 7
0, 1, 2, 3, 4, 5, 6, 7, 8
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
Sign up to request clarification or add additional context in comments.

Comments

0

You should use Arrays.toString, like so:

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] table = new int[11];
        for ( int i = 0; i <=10; i++){
            table[i] = i;
            System.out.println(Arrays.toString(table));
        }
    }
}

However, this will print the entire array, as it is being populated:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 4, 5, 6, 0, 0, 0, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 0, 0, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

If you just want the elements filled so far, it's a little more involved:

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] table = new int[11];
        for ( int i = 0; i <=10; i++){
            table[i] = i;
            for(int j = 0; j <= i; j++)
            {
              System.out.print((j == 0 ? "" : ", ") + table[j]);
            }
            System.out.println();
        }
    }
}

Output:

0
0, 1
0, 1, 2
0, 1, 2, 3
0, 1, 2, 3, 4
0, 1, 2, 3, 4, 5
0, 1, 2, 3, 4, 5, 6
0, 1, 2, 3, 4, 5, 6, 7
0, 1, 2, 3, 4, 5, 6, 7, 8
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

3 Comments

The last one gives 100% expected results and it is still limited to "for" loop (this exercise is to use it) which makes for a perfect solution - thank you very much for your help!
Now I am trying to document every line, so that I can uderstand it better and I am having a hard time figuring out what exactly happens here: ((j == 0 ? "" : ", ") + table[j])
@Mutton It says, "the current element, preceded by a comma and a space, IF this is not the first element on the line"
0

You need two loops, one loop for the rows and then an additional loop for the numbers per row.

for (int i=0; i<=10; i++) {
  table[i] = i;
  for (int j=0; j<=i; j++) {
    System.out.print(table[j]);
  }
  System.out.print("\n");
}

Of course you might need to further format the output to your liking.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.