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;
}
}
}
}
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 .
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);
}
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);
}
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;
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]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.