英文:
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 < 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;
}
}
}
}
答案1
得分: 1
-
原始类型的数组在创建时被填充为默认值(整数类型为
0
,浮点数类型为0.0
/0.0f
,布尔类型为false
),对象类型的数组则被填充为null
。 -
要填充二维数组中的单行,推荐使用
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);
}
- 可以使用循环来填充特定的列:
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);
}
英文:
-
Arrays of primitives are filled with default values (
0
for integer types,0.0
/0.0f
for floating-point types,false
forboolean
) andnull
for objects upon creation. -
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 < arr.length && row >= 0 && null != arr[row]) {
Arrays.fill(arr[row], value);
}
}
public static void zeroRow(int[][] arr, int row) {
fillRow(arr, row, 0);
}
- Specific columns can be filled using loops:
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);
}
答案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数组中的任何一行。考虑之前的twos
和threes
的示例。
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.fill
method.
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<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));
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's
with length of 7
do the following:
int[] fives = IntStream.generate(()-> 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 -> 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]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论