-1

How can i write a shorter code with a for loop, Is it possible? instead of writing;

    int array[][] = new int[10][];
    array[0] = new int[1];
    array[1] = new int[2];
    array[2] = new int[3];
    array[3] = new int[4];
    array[4] = new int[5];
    array[5] = new int[6];
    array[6] = new int[7];
    array[7] = new int[8];
    array[8] = new int[9];
    array[9] = new int[10];

Here is my code;

    int array[][] = new int[10][];
    array[0] = new int[1];
    array[1] = new int[2];
    array[2] = new int[3];
    array[3] = new int[4];
    array[4] = new int[5];
    array[5] = new int[6];
    array[6] = new int[7];
    array[7] = new int[8];
    array[8] = new int[9];
    array[9] = new int[10];

    for (int i = 0; i < 10; i++){
        for (int j = 0; j < i + 1; j++) {
            array[i][j] = i + j;
        }
    }
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < i + 1; j++)
            System.out.printf("%-4d",array[i][j]);
        System.out.println();
    }

}

}

5 Answers5

1

For a generic solution, if you decide to change the length of the array

int array[][] = new int[10][];

for (int i = 0; i < array.length; i++) {
    array[i] = new int[i + 1];
}

You could also combine the two loops like

    for (int i = 0; i < array.length; i++) {
        array[i] = new int[i + 1];
        for (int j = 0; j < i + 1; j++) {
            array[i][j] = i + j;
        }
    }
Viktor Mellgren
  • 4,318
  • 3
  • 42
  • 75
1

You might trying moving the initialization inside the loop

int array[][] = new int[10][];

for (int i = 0; i < 10; i++){
    array[i] = new int[i + 1];
    for (int j = 0; j < i + 1; j++) {
        array[i][j] = i + j;
    }
}
for (int i = 0; i < 10; i++) {
    for (int j = 0; j < i + 1; j++)
        System.out.printf("%-4d",array[i][j]);
    System.out.println();
}
AlanT
  • 3,627
  • 20
  • 28
0

how about this?

for (int i = 0; i < 10; i++){
  array[i] = new int[i + 1]
}

or for the mere fun of making it functional (don't ask about other aspects)

//just the empty array
int[][] array = IntStream.range(0, 10)
                         .map(i -> i + 1)
                         .mapToObj(int[]::new)
                         .toArray(int[][]::new);

Or initialize and fill the array in functional style:

//the array with values
int[][] array = IntStream.range(0, 10)
                         .mapToObj(v -> IntStream.range(v, v + v + 1).toArray())
                         .toArray(int[][]::new);

And even do the output this way:

String out = Arrays.stream(array)
                   .map(x -> Arrays.stream(x)
                                   .mapToObj(y -> String.format("%-4d", y))
                                   .collect(Collectors.joining()))
                   .collect(Collectors.joining("\n"));
System.out.println(out);
Gerald Mücke
  • 10,724
  • 2
  • 50
  • 67
  • i hust wondering what does this symbol -> do? I'm a begginer and i'd like to learn. Thank you – Lena Monika Marshall Jun 03 '16 at 12:10
  • 1
    it defines a lambda expression, you could read it "given the value(s) on the left, do the things on the right". I.e. `i -> i + 1` translates to, `given value i, calculate i+1 (and return it, as map() expects return value)` – Gerald Mücke Jun 03 '16 at 12:25
0

With java 8:

Integer[][] a = new ArrayList<Integer[]>(Collections.nCopies( 10, new Integer[0]  ))
.stream()
.map( p -> new Integer[p.length +1] )
.toArray(size -> new Integer[size][]);  
borjab
  • 11,149
  • 6
  • 71
  • 98
0

code is shorter now thanx :)

    int array[][] = new int[10][];

    for (int i = 0; i < array.length; i++)
    {
        array[i] = new int[i + 1];

        for (int j = 0; j < i + 1; j++) 
        { 
            array[i][j] = i + j;
        }
    }
    for (int[] row : array) 
    {
        for(int k:row)
            System.out.print( k+ " ");
        System.out.println();
    }