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

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

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

问题

  1. public class Assignment12 {
  2. public static void main(String args[]) {
  3. int[][] timestable = new int[10][10];
  4. printSquare(timestable);
  5. zeroRow(timestable, 5);
  6. printSquare(timestable);
  7. int[] arr = timestable[0];
  8. }
  9. public static void printSquare(int[][] arr) {
  10. for (int row = 0; row < 10; row++)
  11. for (int col = 0; col < 10; col++)
  12. arr[row][col] = (row + 1) * (col + 1);
  13. System.out.println();
  14. for (int row = 0; row < 10; row++) {
  15. for (int col = 0; col < arr.length; col++)
  16. System.out.printf("%4d", arr[row][col]);
  17. System.out.println();
  18. }
  19. }
  20. public static void zeroRow(int[][] arr, int M) {
  21. for (int col = 0; col < M; col++) {
  22. if (arr[M][col] != 0) {
  23. arr[M][col] = 0;
  24. }
  25. }
  26. }
  27. }
英文:

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:

  1. public class Assignment12 {
  2. public static void main(String args[]) {
  3. int[][] timestable = new int[10][10];
  4. printSquare(timestable);
  5. zeroRow(timestable, 5);
  6. printSquare(timestable);
  7. int[] arr = timestable[0];
  8. }
  9. public static void printSquare(int[][] arr) {
  10. for (int row = 0; row &lt; 10; row++)
  11. for (int col = 0; col &lt; 10; col++)
  12. arr[row][col] = (row + 1) * (col + 1);
  13. System.out.println();
  14. for (int row = 0; row &lt; 10; row++) {
  15. for (int col = 0; col &lt; arr.length; col++)
  16. System.out.printf(&quot;%4d&quot;, arr[row][col]);
  17. System.out.println();
  18. }
  19. }
  20. public static void zeroRow(int[][] arr, int M) {
  21. for (int col = 0; col &lt; M; col++) {
  22. if (arr[M][col] != 0) {
  23. arr[M][col] = 0;
  24. }
  25. }
  26. }
  27. }

答案1

得分: 1

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

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

  1. public static void fillRow(int[][] arr, int row, int value) {
  2. if (row < arr.length && row >= 0 && null != arr[row]) {
  3. Arrays.fill(arr[row], value);
  4. }
  5. }
  6. public static void zeroRow(int[][] arr, int row) {
  7. fillRow(arr, row, 0);
  8. }
  1. 可以使用循环来填充特定的列:
  1. public static void fillColumn(int[][] arr, int col, int value) {
  2. for (int[] row : arr) {
  3. if (null != row && col >= 0 && col < row.length) {
  4. row[col] = value;
  5. }
  6. }
  7. }
  8. public static void zeroColumn(int[][] arr, int col) {
  9. fillColumn(arr, col, 0);
  10. }
英文:
  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:

  1. public static void fillRow(int[][] arr, int row, int value) {
  2. if (row &lt; arr.length &amp;&amp; row &gt;= 0 &amp;&amp; null != arr[row]) {
  3. Arrays.fill(arr[row], value);
  4. }
  5. }
  6. public static void zeroRow(int[][] arr, int row) {
  7. fillRow(arr, row, 0);
  8. }
  1. Specific columns can be filled using loops:
  1. public static void fillColumn(int[][] arr, int col, int value) {
  2. for (int[] row : arr) {
  3. if (null != row &amp;&amp; col &gt;= 0 &amp;&amp; col &lt; row.length) {
  4. row[col] = value;
  5. }
  6. }
  7. }
  8. public static void zeroColumn(int[][] arr, int col) {
  9. fillColumn(arr, col, 0);
  10. }

答案2

得分: 0

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

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

使用Arrays.fill方法。

  1. int[] tens = new int[5];
  2. Arrays.fill(tens, 10);
  3. System.out.println(Arrays.toString(tens));

输出

  1. [10, 10, 10, 10, 10]

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

  1. BiFunction<Integer, Integer, int[]> fill =
  2. (val, len) -> {
  3. int[] temp = new int[len];
  4. Arrays.fill(temp, val);
  5. return temp;
  6. };
  7. int[] twos = fill.apply(2, 10);
  8. int[] threes = fill.apply(3, 4);
  9. System.out.println(Arrays.toString(twos));
  10. System.out.println(Arrays.toString(threes));

输出

  1. [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
  2. [3, 3, 3, 3]

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

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

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

  1. int[][] b = new int[2][];
  2. b[0] = twos;
  3. 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.

  1. int[] tens = new int[5];
  2. Arrays.fill(tens,10);
  3. System.out.println(Arrays.toString(tens));

Prints

  1. [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.

  1. BiFunction&lt;Integer, Integer, int[]&gt; fill =
  2. (val, len) -&gt; {
  3. int[] temp = new int[len];
  4. Arrays.fill(temp, val);
  5. return temp;};
  6. int[] twos = fill.apply(2,10);
  7. int[] threes = fill.apply(3,4);
  8. System.out.println(Arrays.toString(twos));
  9. System.out.println(Arrays.toString(threes));

prints

  1. [2, 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:

  1. 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.

  1. int[][] b = new int[2][];
  2. b[0] = twos;
  3. b[1] = threes;
  4. </details>
  5. # 答案3
  6. **得分**: 0
  7. 可以按以下方式处理数组:
  8. ```java
  9. public static void fillRow(int[][] arr, int row, int val) {
  10. Arrays.fill(arr[row], val);
  11. }
  1. public static void fillColumn(int[][] arr, int column, int val) {
  2. Arrays.stream(arr).forEach(row -> row[column] = val);
  3. }
  1. public static void main(String[] args) {
  2. int[][] arr = new int[5][5];
  3. fillRow(arr, 3, 3);
  4. fillColumn(arr, 2, 2);
  5. // 输出
  6. Arrays.stream(arr).map(Arrays::toString).forEach(System.out::println);
  7. }

  1. [0, 0, 2, 0, 0]
  2. [0, 0, 2, 0, 0]
  3. [0, 0, 2, 0, 0]
  4. [3, 3, 2, 3, 3]
  5. [0, 0, 2, 0, 0]
英文:

You can process an array as follows:

  1. public static void fillRow(int[][] arr, int row, int val) {
  2. Arrays.fill(arr[row], val);
  3. }
  1. public static void fillColumn(int[][] arr, int column, int val) {
  2. Arrays.stream(arr).forEach(row -&gt; row[column] = val);
  3. }
  1. public static void main(String[] args) {
  2. int[][] arr = new int[5][5];
  3. fillRow(arr, 3, 3);
  4. fillColumn(arr, 2, 2);
  5. // output
  6. Arrays.stream(arr).map(Arrays::toString).forEach(System.out::println);
  7. }

  1. [0, 0, 2, 0, 0]
  2. [0, 0, 2, 0, 0]
  3. [0, 0, 2, 0, 0]
  4. [3, 3, 2, 3, 3]
  5. [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:

确定