如何缩短我的Jagged(Ragged)代码?

时间:2016-06-03 11:46:38

标签: java arrays algorithm jagged-arrays ragged

如何使用for循环编写更短的代码,是否可能?而不是写作;

    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];

这是我的代码;

    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 个答案:

答案 0 :(得分:1)

对于通用解决方案,如果您决定更改数组的长度

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

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

你也可以把两个循环结合起来,比如

    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;
        }
    }

答案 1 :(得分:1)

您可能尝试在循环中移动初始化

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();
}

答案 2 :(得分:0)

怎么样?

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

或仅仅是为了让它成功(不要问其他方面)

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

或初始化以功能样式填充数组:

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

甚至以这种方式输出:

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);

答案 3 :(得分:0)

使用java 8:

 ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));

答案 4 :(得分:0)

代码现在比x更短:)

    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();
    }