自动填充2D数组

时间:2019-09-19 23:26:29

标签: java multidimensional-array

我需要一些使用嵌套的for循环填充2D数组的帮助。 所需的输出是这个...

20.0 19.0 18.0 17.0 16.0 15.0 14.0 13.0 12.0 11.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0 19.0 20.0

19.0 18.0 17.0 16.0 15.0 14.0 13.0 12.0 11.0 10.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0 19.0

18.0 17.0 16.0 15.0 14.0 13.0 12.0 11.0 10.0 9.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0

17.0 16.0 15.0 14.0 13.0 12.0 11.0 10.0 9.0 8.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0

16.0 15.0 14.0 13.0 12.0 11.0 10.0 9.0 8.0 7.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0

15.0 14.0 13.0 12.0 11.0 10.0 9.0 8.0 7.0 6.0 5.0 5.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0

14.0 13.0 12.0 11.0 10.0 9.0 8.0 7.0 6.0 5.0 4.0 5.0 5.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0

13.0 12.0 11.0 10.0 9.0 8.0 7.0 6.0 5.0 4.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0

12.0 11.0 10.0 9.0 8.0 7.0 6.0 5.0 4.0 3.0 2.0 3.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0

11.0 10.0 9.0 8.0 7.0 6.0 5.0 4.0 3.0 2.0 1.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0

10.0 9.0 8.0 7.0 6.0 5.0 4.0 3.0 2.0 1.0 1.0 1.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0

11.0 10.0 9.0 8.0 7.0 6.0 5.0 4.0 3.0 2.0 1.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0

12.0 11.0 10.0 9.0 8.0 7.0 6.0 5.0 4.0 3.0 2.0 3.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0

13.0 12.0 11.0 10.0 9.0 8.0 7.0 6.0 5.0 4.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0

14.0 13.0 12.0 11.0 10.0 9.0 8.0 7.0 6.0 5.0 4.0 5.0 5.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0

15.0 14.0 13.0 12.0 11.0 10.0 9.0 8.0 7.0 6.0 5.0 5.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0

16.0 15.0 14.0 13.0 12.0 11.0 10.0 9.0 8.0 7.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0

17.0 16.0 15.0 14.0 13.0 12.0 11.0 10.0 9.0 8.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0

18.0 17.0 16.0 15.0 14.0 13.0 12.0 11.0 10.0 9.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0

19.0 18.0 17.0 16.0 15.0 14.0 13.0 12.0 11.0 10.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0 19.0

20.0 19.0 18.0 17.0 16.0 15.0 14.0 13.0 12.0 11.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0 19.0 20.0

这是我填写数组的代码...

int row;
        int column;
        int counter=10;
        int counter1=10;
for( row =0; row<array.length; row++) {
            array[row][0] = obj.getZ(10,counter);
            counter--;
            for( column =0; column<array[row].length; column++) {
                array[row][column] = obj.getZ(counter1,10);
                counter1 --;

我已经声明数组为20 x 20,而obj.getZ只是简单地调用此方法...

public double getZ(double x, double y) {

        double z=(Math.abs(x)+Math.abs(y));
        return z;

数组是z值,由x和y的绝对值组成。

2 个答案:

答案 0 :(得分:1)

不是最佳解决方案,但应该可以解决问题

    import java.util.Arrays;

    class Scratch {
        public static void main(String[] args) {

            int[][] arr = new int[21][21];
            int row = 20;
            int column = 0;

            for (int i = 0; i < 21; i ++) {

                column = row;

                for (int j = 0; j < 21; j++) {
                    arr[i][j] = column;

                    if (j < 10) {
                        column = column - 1;
                    } else {
                        column = column + 1;
                    }
                }

                if (i < 10) {
                    row = row - 1;
                } else {
                    row = row + 1;
                }
            }

            for (int[] arrRow : arr) {
                System.out.println(Arrays.toString(arrRow));
            }
        }
    }

结果是:

$ java Scratch
[20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
[17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
[16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
[14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
[13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
[16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
[17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
[18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
[19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

UPD:已修复示例并添加了结果

答案 1 :(得分:0)

您可以使用 IntStream 创建和填充此数组,如下所示:

int m = 10;
int n = 10;
int[][] arr = IntStream.rangeClosed(-m, m)
        .mapToObj(i -> IntStream.rangeClosed(-n, n)
                .map(j -> Math.abs(i) + Math.abs(j))
                .toArray())
        .toArray(int[][]::new);
// output
Arrays.stream(arr).map(row -> Arrays.stream(row)
        .mapToObj(String::valueOf)
        .collect(Collectors.joining(" ")))
        .forEach(System.out::println);
20 19 18 17 16 15 14 13 12 11 10 11 12 13 14 15 16 17 18 19 20
19 18 17 16 15 14 13 12 11 10 9 10 11 12 13 14 15 16 17 18 19
18 17 16 15 14 13 12 11 10 9 8 9 10 11 12 13 14 15 16 17 18
17 16 15 14 13 12 11 10 9 8 7 8 9 10 11 12 13 14 15 16 17
16 15 14 13 12 11 10 9 8 7 6 7 8 9 10 11 12 13 14 15 16
15 14 13 12 11 10 9 8 7 6 5 6 7 8 9 10 11 12 13 14 15
14 13 12 11 10 9 8 7 6 5 4 5 6 7 8 9 10 11 12 13 14
13 12 11 10 9 8 7 6 5 4 3 4 5 6 7 8 9 10 11 12 13
12 11 10 9 8 7 6 5 4 3 2 3 4 5 6 7 8 9 10 11 12
11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11
10 9 8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8 9 10
11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11
12 11 10 9 8 7 6 5 4 3 2 3 4 5 6 7 8 9 10 11 12
13 12 11 10 9 8 7 6 5 4 3 4 5 6 7 8 9 10 11 12 13
14 13 12 11 10 9 8 7 6 5 4 5 6 7 8 9 10 11 12 13 14
15 14 13 12 11 10 9 8 7 6 5 6 7 8 9 10 11 12 13 14 15
16 15 14 13 12 11 10 9 8 7 6 7 8 9 10 11 12 13 14 15 16
17 16 15 14 13 12 11 10 9 8 7 8 9 10 11 12 13 14 15 16 17
18 17 16 15 14 13 12 11 10 9 8 9 10 11 12 13 14 15 16 17 18
19 18 17 16 15 14 13 12 11 10 9 10 11 12 13 14 15 16 17 18 19
20 19 18 17 16 15 14 13 12 11 10 11 12 13 14 15 16 17 18 19 20

对于 double[][] 数组,它的工作原理相同:

int m = 10;
int n = 10;
double[][] arr = IntStream.rangeClosed(-m, m)
        .mapToObj(i -> IntStream.rangeClosed(-n, n)
                .mapToDouble(j -> Math.abs(i) + Math.abs(j))
                .toArray())
        .toArray(double[][]::new);
// formatted output
Arrays.stream(arr).map(row -> Arrays.stream(row)
        .mapToObj(i -> String.format("%4.1f", i))
        .collect(Collectors.joining(" ")))
        .forEach(System.out::println);
20,0 19,0 18,0 17,0 16,0 15,0 14,0 13,0 12,0 11,0 10,0 11,0 12,0 13,0 14,0 15,0 16,0 17,0 18,0 19,0 20,0
19,0 18,0 17,0 16,0 15,0 14,0 13,0 12,0 11,0 10,0  9,0 10,0 11,0 12,0 13,0 14,0 15,0 16,0 17,0 18,0 19,0
18,0 17,0 16,0 15,0 14,0 13,0 12,0 11,0 10,0  9,0  8,0  9,0 10,0 11,0 12,0 13,0 14,0 15,0 16,0 17,0 18,0
17,0 16,0 15,0 14,0 13,0 12,0 11,0 10,0  9,0  8,0  7,0  8,0  9,0 10,0 11,0 12,0 13,0 14,0 15,0 16,0 17,0
16,0 15,0 14,0 13,0 12,0 11,0 10,0  9,0  8,0  7,0  6,0  7,0  8,0  9,0 10,0 11,0 12,0 13,0 14,0 15,0 16,0
15,0 14,0 13,0 12,0 11,0 10,0  9,0  8,0  7,0  6,0  5,0  6,0  7,0  8,0  9,0 10,0 11,0 12,0 13,0 14,0 15,0
14,0 13,0 12,0 11,0 10,0  9,0  8,0  7,0  6,0  5,0  4,0  5,0  6,0  7,0  8,0  9,0 10,0 11,0 12,0 13,0 14,0
13,0 12,0 11,0 10,0  9,0  8,0  7,0  6,0  5,0  4,0  3,0  4,0  5,0  6,0  7,0  8,0  9,0 10,0 11,0 12,0 13,0
12,0 11,0 10,0  9,0  8,0  7,0  6,0  5,0  4,0  3,0  2,0  3,0  4,0  5,0  6,0  7,0  8,0  9,0 10,0 11,0 12,0
11,0 10,0  9,0  8,0  7,0  6,0  5,0  4,0  3,0  2,0  1,0  2,0  3,0  4,0  5,0  6,0  7,0  8,0  9,0 10,0 11,0
10,0  9,0  8,0  7,0  6,0  5,0  4,0  3,0  2,0  1,0  0,0  1,0  2,0  3,0  4,0  5,0  6,0  7,0  8,0  9,0 10,0
11,0 10,0  9,0  8,0  7,0  6,0  5,0  4,0  3,0  2,0  1,0  2,0  3,0  4,0  5,0  6,0  7,0  8,0  9,0 10,0 11,0
12,0 11,0 10,0  9,0  8,0  7,0  6,0  5,0  4,0  3,0  2,0  3,0  4,0  5,0  6,0  7,0  8,0  9,0 10,0 11,0 12,0
13,0 12,0 11,0 10,0  9,0  8,0  7,0  6,0  5,0  4,0  3,0  4,0  5,0  6,0  7,0  8,0  9,0 10,0 11,0 12,0 13,0
14,0 13,0 12,0 11,0 10,0  9,0  8,0  7,0  6,0  5,0  4,0  5,0  6,0  7,0  8,0  9,0 10,0 11,0 12,0 13,0 14,0
15,0 14,0 13,0 12,0 11,0 10,0  9,0  8,0  7,0  6,0  5,0  6,0  7,0  8,0  9,0 10,0 11,0 12,0 13,0 14,0 15,0
16,0 15,0 14,0 13,0 12,0 11,0 10,0  9,0  8,0  7,0  6,0  7,0  8,0  9,0 10,0 11,0 12,0 13,0 14,0 15,0 16,0
17,0 16,0 15,0 14,0 13,0 12,0 11,0 10,0  9,0  8,0  7,0  8,0  9,0 10,0 11,0 12,0 13,0 14,0 15,0 16,0 17,0
18,0 17,0 16,0 15,0 14,0 13,0 12,0 11,0 10,0  9,0  8,0  9,0 10,0 11,0 12,0 13,0 14,0 15,0 16,0 17,0 18,0
19,0 18,0 17,0 16,0 15,0 14,0 13,0 12,0 11,0 10,0  9,0 10,0 11,0 12,0 13,0 14,0 15,0 16,0 17,0 18,0 19,0
20,0 19,0 18,0 17,0 16,0 15,0 14,0 13,0 12,0 11,0 10,0 11,0 12,0 13,0 14,0 15,0 16,0 17,0 18,0 19,0 20,0

另见:How to print a given diamond pattern?