Java 如何打印出包含所有值的数组?
我尝试打印矩阵AxA,其中显示了所有值,但失败了 输出应该如下所示:Java 如何打印出包含所有值的数组?,java,arrays,Java,Arrays,我尝试打印矩阵AxA,其中显示了所有值,但失败了 输出应该如下所示: 1 2 3 4 5 6 多谢各位 这是我的代码: import java.util.Scanner; public class Lab2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a number for rows: "); int ro
1 2 3
4 5 6
多谢各位
这是我的代码:
import java.util.Scanner;
public class Lab2 {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a number for rows: ");
int rows = input.nextInt();
System.out.print("Enter a number for columns: ");
int columns = input.nextInt();
int[][] array = new int[rows][columns];
System.out.println("Enter the numbers in array: ");
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
array[i][j] = input.nextInt();
}
System.out.println(" " + array);
}
}
这是我现在的代码:
import java.util.Scanner;
public class Lab2 {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a number for rows: ");
int rows = input.nextInt();
System.out.print("Enter a number for columns: ");
int columns = input.nextInt();
int[][] array = new int[rows][columns];
System.out.println("Enter the numbers in array: ");
for(int i=0 ; i<rows ; i++)
{
for(int j=0 ; j<columns ; j++)
{
array[i][j] = input.nextInt();
}
}
for(int i=0 ; i<rows ; i++)
{
for(int j=0 ; j<columns ; j++)
{
System.out.print(array[i][j] + " , ");
}
System.out.println("\n");
}
}
public static doube averageRow(int[][] array) {
int rowTotal = 0;
double average = 0;
for (int rows = 0; rows < array.length; rows++) {
for (int columns = 0; columns < array[rows].length; columns++) {
rowTotal += array[rows][columns];
}
average = rowTotal / array[rows].length;
System.out.println(average);
rowTotal = 0;
}
return rowTotal;
}
public static doube averageColumn(int[][] array) {
int columnTotal = 0;
double average = 0;
for (int columns = 0; columns < array.length; columns++) {
for (int rows = 0; rows < array[columns].length; rows++) {
columnTotal += array[rows][columns];
}
average = columnTotal / array[columns].length;
System.out.println(average);
columnTotal = 0;
}
return columnTotal;
}
}
import java.util.Scanner;
公共类Lab2{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
System.out.print(“为行输入数字:”);
int rows=input.nextInt();
System.out.print(“为列输入一个数字:”);
int columns=input.nextInt();
int[][]数组=新的int[行][列];
System.out.println(“在数组中输入数字:”);
对于(inti=0;i您可以使用相同的逻辑来读取和写入数组元素,请尝试以下代码
正在读取数组
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
{
array[i][j] = input.nextInt();
}
}
for(int i=0;i请记住,不能简单地打印整个数组,即:System.out.println(“+array”)
您的思路是正确的,但需要使用for循环打印(请尝试printf,它很强大):
for(int i=0;i
使用
将打印:
[1 ,2, 3]
[4, 5, 6]
或
我可以想到另一种单循环方式:
for (int[] a : array) {
String str = Arrays.toString(a);
System.out.println(str.substring(1, str.length()-1).replaceAll("[,]", ""));
}
for循环基本上迭代每一行,即“a”的值是矩阵中的一个数组(行)。
Arrays.toString(a)返回与数组等价的字符串,例如,对于a=[1,2,3,4,5],它将返回“[1,2,3,4,5]”。现在只需删除括号(子字符串)并将逗号(“,”)替换为“”,即可得到所需的结果。
请注意,replaceAll的第一个参数是正则表达式,而不是字符串本身。在提出此类问题之前,请使用搜索。
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++){
System.out.printf("%d ", array[i][j]);
}
System.out.printf("\n");
}
Stream.of(array).map(Arrays::toString).forEach(System.out::println);
[1 ,2, 3]
[4, 5, 6]
System.out.println(Arrays.deepToString(array));
for (int[] a : array) {
String str = Arrays.toString(a);
System.out.println(str.substring(1, str.length()-1).replaceAll("[,]", ""));
}