如何在Java中将数组中的整行设为一个值?

huangapple go评论73阅读模式
英文:

How would I make an entire row in an array one value in Java?

问题

public class Assignment12 {
    public static void main(String args[]) {
        int[][] timestable = new int[10][10];
        printSquare(timestable);
        zeroRow(timestable, 5);
        printSquare(timestable);
        int[] arr = timestable[0];
    }

    public static void printSquare(int[][] arr) {
        for (int row = 0; row < 10; row++)
            for (int col = 0; col < 10; col++)
                arr[row][col] = (row + 1) * (col + 1);

        System.out.println();
        for (int row = 0; row < 10; row++) {
            for (int col = 0; col < arr.length; col++)
                System.out.printf("%4d", arr[row][col]);
            System.out.println();
        }
    }

    public static void zeroRow(int[][] arr, int M) {
        for (int col = 0; col < M; col++) {
            if (arr[M][col] != 0) {
                arr[M][col] = 0;
            }
        }
    }
}
英文:

How would I make an entire row (or column) one value in an array? For example, I have a row of all random numbers, and I want to set all of these values to 0.

Here is what I have so far:

public class Assignment12 {
    public static void main(String args[]) {
        int[][] timestable = new int[10][10];
        printSquare(timestable);
        zeroRow(timestable, 5);
        printSquare(timestable);
        int[] arr = timestable[0];
    }

    public static void printSquare(int[][] arr) {
        for (int row = 0; row &lt; 10; row++)
            for (int col = 0; col &lt; 10; col++)
                arr[row][col] = (row + 1) * (col + 1);

        System.out.println();
        for (int row = 0; row &lt; 10; row++) {
            for (int col = 0; col &lt; arr.length; col++)
                System.out.printf(&quot;%4d&quot;, arr[row][col]);
            System.out.println();
        }
    }

    public static void zeroRow(int[][] arr, int M) {
        for (int col = 0; col &lt; M; col++) {
            if (arr[M][col] != 0) {
                arr[M][col] = 0;
            }
        }
    }
}

答案1

得分: 1

  1. 原始类型的数组在创建时被填充为默认值(整数类型为0,浮点数类型为0.0/0.0f,布尔类型为false),对象类型的数组则被填充为null

  2. 要填充二维数组中的单行,推荐使用Arrays.fill方法:

public static void fillRow(int[][] arr, int row, int value) {
    if (row < arr.length && row >= 0 && null != arr[row]) {
        Arrays.fill(arr[row], value);
    }
}

public static void zeroRow(int[][] arr, int row) {
    fillRow(arr, row, 0);
}
  1. 可以使用循环来填充特定的列:
public static void fillColumn(int[][] arr, int col, int value) {
    for (int[] row : arr) {
        if (null != row && col >= 0 && col < row.length) {
            row[col] = value;
        } 
    }
}

public static void zeroColumn(int[][] arr, int col) {
    fillColumn(arr, col, 0);
}
英文:
  1. Arrays of primitives are filled with default values (0 for integer types, 0.0/0.0f for floating-point types, false for boolean) and null for objects upon creation.

  2. To populate a single row in a 2D array, it's recommended to use Arrays.fill method:

public static void fillRow(int[][] arr, int row, int value) {
    if (row &lt; arr.length &amp;&amp; row &gt;= 0 &amp;&amp; null != arr[row]) {
        Arrays.fill(arr[row], value);
    }
}

public static void zeroRow(int[][] arr, int row) {
    fillRow(arr, row, 0);
}
  1. Specific columns can be filled using loops:
public static void fillColumn(int[][] arr, int col, int value) {
    for (int[] row : arr) {
        if (null != row &amp;&amp; col &gt;= 0 &amp;&amp; col &lt; row.length) {
            row[col] = value;
        } 
    }
}

public static void zeroColumn(int[][] arr, int col) {
    fillColumn(arr, col, 0);
}

答案2

得分: 0

> 如何在Java中使数组中的整行成为一个值?

默认情况下,具有固定长度的原始int数组会被初始化为0。要用其他值填充它们,可以按以下步骤进行。

使用Arrays.fill方法。

int[] tens = new int[5];
Arrays.fill(tens, 10);

System.out.println(Arrays.toString(tens));

输出

[10, 10, 10, 10, 10]

或者您可以编写一个lambda表达式,并在需要填充数组时调用它。它还会为您创建数组,因为您提供了长度。

BiFunction<Integer, Integer, int[]> fill =
    (val, len) -> {
        int[] temp = new int[len];
        Arrays.fill(temp, val);
        return temp;
    };

int[] twos = fill.apply(2, 10);
int[] threes = fill.apply(3, 4);

System.out.println(Arrays.toString(twos));
System.out.println(Arrays.toString(threes));

输出

[2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
[3, 3, 3, 3]

或者,在紧急情况下,可以从IntStream生成一个数组。要创建长度为7的值为5的数组,请执行以下操作:

int[] fives = IntStream.generate(() -> 5).limit(7).toArray();

注意:一旦您获得线性数组,还可以将其分配给2D数组中的任何一行。考虑之前的twosthrees的示例。

int[][] b = new int[2][];
b[0] = twos;
b[1] = threes;
英文:

> How would I make an entire row in an array one value in Java?

By default, primitive int arrays of a fixed length are initialized to 0's. To fill them with other values you can do the following.

Use the Arrays.fillmethod.

int[] tens = new int[5];
Arrays.fill(tens,10);

System.out.println(Arrays.toString(tens));

Prints

[10, 10, 10, 10, 10]

Or you can write a lambda and call it when you need to fill an array. It will also create the array for you since you supply the length.

BiFunction&lt;Integer, Integer, int[]&gt; fill =
		(val, len) -&gt; {
                  int[] temp = new int[len];
                  Arrays.fill(temp, val);
                  return temp;};
		
int[] twos = fill.apply(2,10);
int[] threes = fill.apply(3,4);

System.out.println(Arrays.toString(twos));
System.out.println(Arrays.toString(threes));

prints

[2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
[3, 3, 3, 3]

Or, in a pinch, generate one from an IntStream. For an array of 5&#39;s with length of 7 do the following:

int[] fives = IntStream.generate(()-&gt; 5).limit(7).toArray();

Note: Once you have your linear array you can also assign it to any row in a 2D array. Consider the previous examples of twos and threes.

int[][] b = new int[2][];
b[0] = twos;
b[1] = threes;



</details>



# 答案3
**得分**: 0

可以按以下方式处理数组:

```java
public static void fillRow(int[][] arr, int row, int val) {
    Arrays.fill(arr[row], val);
}
public static void fillColumn(int[][] arr, int column, int val) {
    Arrays.stream(arr).forEach(row -> row[column] = val);
}
public static void main(String[] args) {
    int[][] arr = new int[5][5];
    fillRow(arr, 3, 3);
    fillColumn(arr, 2, 2);

    // 输出
    Arrays.stream(arr).map(Arrays::toString).forEach(System.out::println);
}

[0, 0, 2, 0, 0]
[0, 0, 2, 0, 0]
[0, 0, 2, 0, 0]
[3, 3, 2, 3, 3]
[0, 0, 2, 0, 0]
英文:

You can process an array as follows:

public static void fillRow(int[][] arr, int row, int val) {
    Arrays.fill(arr[row], val);
}
public static void fillColumn(int[][] arr, int column, int val) {
    Arrays.stream(arr).forEach(row -&gt; row[column] = val);
}
public static void main(String[] args) {
    int[][] arr = new int[5][5];
    fillRow(arr, 3, 3);
    fillColumn(arr, 2, 2);

    // output
    Arrays.stream(arr).map(Arrays::toString).forEach(System.out::println);
}

[0, 0, 2, 0, 0]
[0, 0, 2, 0, 0]
[0, 0, 2, 0, 0]
[3, 3, 2, 3, 3]
[0, 0, 2, 0, 0]

huangapple
  • 本文由 发表于 2020年10月5日 04:35:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64199625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定