Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
二维数组将行打印为列java_Java_Arrays - Fatal编程技术网

二维数组将行打印为列java

二维数组将行打印为列java,java,arrays,Java,Arrays,我们被要求打印这个二维数组,列作为行 例如:第一列是20,11,27,必须打印: 20 11 二十七 这是我到目前为止的代码,我甚至不能让它正常打印列,你们知道问题是什么吗?如果你们能帮我找到解决问题的方法 public class TwoDimensionalArrays { public static void main (String args[]) { final int size1 = 2, size2 = 4, size3 = 5; int [][] numbers

我们被要求打印这个二维数组,列作为行

例如:第一列是20,11,27,必须打印:

20
11
二十七

这是我到目前为止的代码,我甚至不能让它正常打印列,你们知道问题是什么吗?如果你们能帮我找到解决问题的方法

public class TwoDimensionalArrays
{
public static void main (String args[])
{
    final int size1 = 2, size2 = 4, size3 = 5;
    int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};        
    int row = 0, col = 0;

        for(row = 0; row <= size1; row++); //loops through rows
        {
            for(col = 0; col <= size2; col++); //loops through columns
            {
                System.out.println(numbers[row][col]);
            }
        System.out.print("\n"); //takes a new line before each new print
        }
    }
 }
公共类二维数组
{
公共静态void main(字符串参数[])
{
最终整数大小1=2,大小2=4,大小3=5;
int[]数字={20,25,34,19,33},{11,17,15,45,26},{27,22,9,41,13};
int行=0,列=0;

对于(row=0;row您不应该依赖于多维数组的某些预定义大小(更好的名称是数组数组数组)。始终使用数组的实际大小,如
numbers.length
numbers[0]。length
或用于以下各项:

int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};        

for (int[] row: numbers){
    for (int num: row){
        System.out.print(num+" ");
    }
    System.out.println();
}
    for(int i = 0; i < numbers[0].length; i++) {
        for(int j = 0; j < numbers.length; j++) {
            System.out.print(numbers[j][i]+"\t");
        }
        System.out.println();
    }
int maxDigits = 2; //provided or calculated
int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};        
for( int[] row : numbers ) {
  for( int n : row) {
    System.out.print( String.format("%" + maxDigits + "d ", n) ); //creates the format string "%2d" for 2 digits maximum
  }
  System.out.println(); //takes a new line before each new print
}
20 25 34 19 33 
11 17 15 45 26 
27 22  9 41 13 
结果是:

20 25 34 19 33 
11 17 15 45 26 
27 22 9 41 13 
如果你想转换,你可以这样做:

int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};        

for (int[] row: numbers){
    for (int num: row){
        System.out.print(num+" ");
    }
    System.out.println();
}
    for(int i = 0; i < numbers[0].length; i++) {
        for(int j = 0; j < numbers.length; j++) {
            System.out.print(numbers[j][i]+"\t");
        }
        System.out.println();
    }
int maxDigits = 2; //provided or calculated
int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};        
for( int[] row : numbers ) {
  for( int n : row) {
    System.out.print( String.format("%" + maxDigits + "d ", n) ); //creates the format string "%2d" for 2 digits maximum
  }
  System.out.println(); //takes a new line before each new print
}
20 25 34 19 33 
11 17 15 45 26 
27 22  9 41 13 

注意:数组数组中没有行和列,只有维度。

您不应该依赖多维数组的一些预定义大小(更好的名称是数组数组)。始终使用数组的实际大小,如
数字。长度
数字[0].length
或用于以下各项:

int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};        

for (int[] row: numbers){
    for (int num: row){
        System.out.print(num+" ");
    }
    System.out.println();
}
    for(int i = 0; i < numbers[0].length; i++) {
        for(int j = 0; j < numbers.length; j++) {
            System.out.print(numbers[j][i]+"\t");
        }
        System.out.println();
    }
int maxDigits = 2; //provided or calculated
int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};        
for( int[] row : numbers ) {
  for( int n : row) {
    System.out.print( String.format("%" + maxDigits + "d ", n) ); //creates the format string "%2d" for 2 digits maximum
  }
  System.out.println(); //takes a new line before each new print
}
20 25 34 19 33 
11 17 15 45 26 
27 22  9 41 13 
结果是:

20 25 34 19 33 
11 17 15 45 26 
27 22 9 41 13 
如果你想转换,你可以这样做:

int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};        

for (int[] row: numbers){
    for (int num: row){
        System.out.print(num+" ");
    }
    System.out.println();
}
    for(int i = 0; i < numbers[0].length; i++) {
        for(int j = 0; j < numbers.length; j++) {
            System.out.print(numbers[j][i]+"\t");
        }
        System.out.println();
    }
int maxDigits = 2; //provided or calculated
int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};        
for( int[] row : numbers ) {
  for( int n : row) {
    System.out.print( String.format("%" + maxDigits + "d ", n) ); //creates the format string "%2d" for 2 digits maximum
  }
  System.out.println(); //takes a new line before each new print
}
20 25 34 19 33 
11 17 15 45 26 
27 22  9 41 13 

注意:数组数组中没有行和列,只有维度。

删除
在循环的末尾

像这样:

for (row = 0; row <= size1; row++) //loops through rows
 {
   for (col = 0; col <= size2; col++) //loops through columns
    {
       System.out.print(numbers[row][col]+" ");
     }
    System.out.print("\n"); //takes a new line before each new print
  }

删除循环末尾的

像这样:

for (row = 0; row <= size1; row++) //loops through rows
 {
   for (col = 0; col <= size2; col++) //loops through columns
    {
       System.out.print(numbers[row][col]+" ");
     }
    System.out.print("\n"); //takes a new line before each new print
  }

您不需要直接提供尺寸,但可能需要计算(或者,为了更简单,提供)元素的最大长度

然后,打印可以如下所示:

int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};        

for (int[] row: numbers){
    for (int num: row){
        System.out.print(num+" ");
    }
    System.out.println();
}
    for(int i = 0; i < numbers[0].length; i++) {
        for(int j = 0; j < numbers.length; j++) {
            System.out.print(numbers[j][i]+"\t");
        }
        System.out.println();
    }
int maxDigits = 2; //provided or calculated
int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};        
for( int[] row : numbers ) {
  for( int n : row) {
    System.out.print( String.format("%" + maxDigits + "d ", n) ); //creates the format string "%2d" for 2 digits maximum
  }
  System.out.println(); //takes a new line before each new print
}
20 25 34 19 33 
11 17 15 45 26 
27 22  9 41 13 
请注意,您应该使用
System.out.print()
进行无换行打印

然后,输出将如下所示:

int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};        

for (int[] row: numbers){
    for (int num: row){
        System.out.print(num+" ");
    }
    System.out.println();
}
    for(int i = 0; i < numbers[0].length; i++) {
        for(int j = 0; j < numbers.length; j++) {
            System.out.print(numbers[j][i]+"\t");
        }
        System.out.println();
    }
int maxDigits = 2; //provided or calculated
int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};        
for( int[] row : numbers ) {
  for( int n : row) {
    System.out.print( String.format("%" + maxDigits + "d ", n) ); //creates the format string "%2d" for 2 digits maximum
  }
  System.out.println(); //takes a new line before each new print
}
20 25 34 19 33 
11 17 15 45 26 
27 22  9 41 13 

您不需要直接提供尺寸,但可能需要计算(或者,为了更简单,提供)元素的最大长度

然后,打印可以如下所示:

int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};        

for (int[] row: numbers){
    for (int num: row){
        System.out.print(num+" ");
    }
    System.out.println();
}
    for(int i = 0; i < numbers[0].length; i++) {
        for(int j = 0; j < numbers.length; j++) {
            System.out.print(numbers[j][i]+"\t");
        }
        System.out.println();
    }
int maxDigits = 2; //provided or calculated
int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};        
for( int[] row : numbers ) {
  for( int n : row) {
    System.out.print( String.format("%" + maxDigits + "d ", n) ); //creates the format string "%2d" for 2 digits maximum
  }
  System.out.println(); //takes a new line before each new print
}
20 25 34 19 33 
11 17 15 45 26 
27 22  9 41 13 
请注意,您应该使用
System.out.print()
进行无换行打印

然后,输出将如下所示:

int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};        

for (int[] row: numbers){
    for (int num: row){
        System.out.print(num+" ");
    }
    System.out.println();
}
    for(int i = 0; i < numbers[0].length; i++) {
        for(int j = 0; j < numbers.length; j++) {
            System.out.print(numbers[j][i]+"\t");
        }
        System.out.println();
    }
int maxDigits = 2; //provided or calculated
int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};        
for( int[] row : numbers ) {
  for( int n : row) {
    System.out.print( String.format("%" + maxDigits + "d ", n) ); //creates the format string "%2d" for 2 digits maximum
  }
  System.out.println(); //takes a new line before each new print
}
20 25 34 19 33 
11 17 15 45 26 
27 22  9 41 13 
公共类rwr{
公共静态void main(字符串[]args){
最终整数大小1=2,大小2=4,大小3=5;
int[]number={{34,34,33,33,44},{22,23,24,23,24},{23,44,55,66,66};
int行=0,列=0;
对于(行=0;行<代码>公共类rwr{
公共静态void main(字符串[]args){
最终整数大小1=2,大小2=4,大小3=5;
int[]number={{34,34,33,33,44},{22,23,24,23,24},{23,44,55,66,66};
int行=0,列=0;

对于(row=0;row闻起来非常像家庭作业:P
System.out.println
已经添加了一个换行符,这是
ln
除了表示的
print
之外还表示的内容。对于打印列,使用System.out.print(colValue+“\t”);->列之间的制表符空间,并且一旦所有列都打印完毕,使用System.out.println();->新线路这不是家庭作业哈哈,这只是讲师让我们研究的东西,看看我们是否可以开始工作,我喜欢阵列的工作方式,所以我有兴趣让它工作:P@PrakharMohanSrivastava当前位置这有什么关系吗?他发布了自己的努力,并提出了一个问题。即使是家庭作业也没关系。家庭作业非常像家庭作业k:P
System.out.println
已经添加了一个换行符,这是除了
print
之外,
ln
所代表的内容。对于打印列,请使用System.out.print(colValue+“\t”);->列之间的制表符空间,并且在打印完所有列后,使用System.out.println();->新线路这不是家庭作业哈哈,这只是讲师让我们研究的东西,看看我们是否可以开始工作,我喜欢阵列的工作方式,所以我有兴趣让它工作:P@PrakharMohanSrivastava:你介意吗?他公布了自己的努力,并提出了一个问题……即使是家庭作业也没关系,所以现在我把它打印好了,唯一的问题是直接打印列。我们的讲师要求我们找到一种方法打印出来,这样,如果你明白我的意思,行就是列?为帮助打印出来干杯,伙计们,我不敢相信这是一个讨厌的分号Haait从你原来的问题中不太清楚。我已经更新了我的答案。好的,所以不我把它打印好了,唯一的问题是它把列直接打印出来。我们的讲师让我们想办法把它打印出来,这样行就是列,如果你明白我的意思的话?为帮助打印出来干杯,伙计们,我不敢相信这是一个讨厌的分号hahaIT跟你原来的问题不太清楚。我明白了注明我的答案。