如何以矩阵框格式打印出简单的int [] [],就像我们手写矩阵的格式一样。简单的循环运行显然不起作用。如果它有助于我尝试在linux ssh终端中编译此代码。
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.println(matrix[i][j] + " ");
}
System.out.println();
}
答案 0 :(得分:30)
final int[][] matrix = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
产地:
1 2 3
4 5 6
7 8 9
答案 1 :(得分:19)
要正确设置列中的数字格式,最好使用 printf 。根据最大或最小数字的大小,您可能需要调整模式"%4d"
。例如,要允许Integer.MIN_VALUE
和Integer.MAX_VALUE
之间的任何整数,请使用"%12d"
。
public void printMatrix(int[][] matrix) {
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++) {
System.out.printf("%4d", matrix[row][col]);
}
System.out.println();
}
}
示例输出:
36 913 888 908
732 626 61 237
5 8 50 265
192 232 129 307
答案 2 :(得分:13)
int[][] matrix = {
{1,2,3},
{4,5,6},
{7,8,9},
{10,11,12}
};
printMatrix(matrix);
public void printMatrix(int[][] m){
try{
int rows = m.length;
int columns = m[0].length;
String str = "|\t";
for(int i=0;i<rows;i++){
for(int j=0;j<columns;j++){
str += m[i][j] + "\t";
}
System.out.println(str + "|");
str = "|\t";
}
}catch(Exception e){System.out.println("Matrix is empty!!");}
}
输出:
| 1 2 3 |
| 4 5 6 |
| 7 8 9 |
| 10 11 12 |
答案 3 :(得分:3)
int[][] matrix = {
{1,2,3},
{4,5,6},
{7,8,9}
};
//use foreach loop as below to avoid IndexOutOfBoundException
//need to check matrix != null if implements as a method
//for each row in the matrix
for (int[] row : matrix) {
//for each number in the row
for (int j : row) {
System.out.print(j + " ");
}
System.out.println("");
}
答案 4 :(得分:3)
以java8方式:
import java.util.Arrays;
public class MatrixPrinter {
public static void main(String[] args) {
final int[][] matrix = new int[4][4];
printMatrix(matrix);
}
public static void printMatrix(int[][] matrix) {
Arrays.stream(matrix)
.forEach(
(row) -> {
System.out.print("[");
Arrays.stream(row)
.forEach((el) -> System.out.print(" " + el + " "));
System.out.println("]");
}
);
}
}
这会产生
[ 0 0 0 0 ]
[ 0 0 0 0 ]
[ 0 0 0 0 ]
[ 0 0 0 0 ]
但是既然我们在这里为什么不让行布局可以自定义呢?
我们需要的是将lamba传递给matrixPrinter方法:
import java.util.Arrays;
import java.util.function.Consumer;
public class MatrixPrinter {
public static void main(String[] args) {
final int[][] matrix = new int[3][3];
Consumer<int[]> noDelimiter = (row) -> {
Arrays.stream(row).forEach((el) -> System.out.print(" " + el + " "));
System.out.println();
};
Consumer<int[]> pipeDelimiter = (row) -> {
Arrays.stream(row).forEach((el) -> System.out.print("| " + el + " "));
System.out.println("|");
};
Consumer<int[]> likeAList = (row) -> {
System.out.print("[");
Arrays.stream(row)
.forEach((el) -> System.out.print(" " + el + " "));
System.out.println("]");
};
printMatrix(matrix, noDelimiter);
System.out.println();
printMatrix(matrix, pipeDelimiter);
System.out.println();
printMatrix(matrix, likeAList);
}
public static void printMatrix(int[][] matrix, Consumer<int[]> rowPrinter) {
Arrays.stream(matrix)
.forEach((row) -> rowPrinter.accept(row));
}
}
这是结果:
0 0 0
0 0 0
0 0 0
| 0 | 0 | 0 |
| 0 | 0 | 0 |
| 0 | 0 | 0 |
[ 0 0 0 ]
[ 0 0 0 ]
[ 0 0 0 ]
答案 5 :(得分:1)
public static void printMatrix(double[][] matrix) {
for (double[] row : matrix) {
for (double element : row) {
System.out.printf("%5.1f", element);
}
System.out.println();
}
}
printMatrix(new double[][]{2,0,0},{0,2,0},{0,0,3}});
2.0 0.0 0.0
0.0 2.0 0.0
0.0 0.0 3.0
答案 6 :(得分:0)
public class Matrix
{
public static void main(String[] args)
{
double Matrix [] []={
{0*1,0*2,0*3,0*4),
{0*1,1*1,2*1,3*1),
{0*2,1*2,2*2,3*2),
{0*3,1*3,2*3,3*3)
};
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
System.out.print(Matrix [i] [j] + " ");
System.out.println();
}
}
}
答案 7 :(得分:0)
这是我使用StringBuilder数组显示二维整数数组的有效方法。
public static void printMatrix(int[][] arr) {
if (null == arr || arr.length == 0) {
// empty or null matrix
return;
}
int idx = -1;
StringBuilder[] sbArr = new StringBuilder[arr.length];
for (int[] row : arr) {
sbArr[++idx] = new StringBuilder("(\t");
for (int elem : row) {
sbArr[idx].append(elem + "\t");
}
sbArr[idx].append(")");
}
for (int i = 0; i < sbArr.length; i++) {
System.out.println(sbArr[i]);
}
System.out.println("\nDONE\n");
}
答案 8 :(得分:0)
public static void main(String[] args)
{
int [] [] ar=
{
{12,33,23},
{34,56,75},
{14,76,89},
{45,87,20}
};
我更喜欢在Java中使用增强型循环
由于我们的ar
是数组 [2D] 。因此,当您迭代它时,您将首先获得一个数组,然后您可以迭代array
来获取单个元素。
for(int[] num: ar)
{
for(int ele : num)
{
System.out.print(" " +ele);
}
System.out.println(" " );
}
}
答案 9 :(得分:0)
自Java 8:
Sub Toggle_Row_Field()
'Add/Remove the field to the pivot table.
'The field is determined by the button text that calls the macro.
Dim pt As PivotTable
Dim pf As PivotField
Dim sField As String
Dim shp As Shape
'Set variables
Set pt = ActiveSheet.PivotTables(1)
Set shp = ActiveSheet.Shapes(Application.Caller)
sField = shp.TextFrame.Characters.Text
'Toggle field
'If visible then hide it
If pt.PivotFields(sField).Orientation = xlRowField Then
pt.PivotFields(sField).Orientation = xlHidden
shp.Fill.ForeColor.Brightness = 0.5
Else 'Add to Rows area
pt.PivotFields(sField).Orientation = xlRowField
shp.Fill.ForeColor.Brightness = 0
End If
End Sub
输出:
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Arrays.stream(matrix).map(Arrays::toString).forEach(System.out::println);