Java 2d数组列正在打印两次

Java 2d数组列正在打印两次,java,arrays,string,Java,Arrays,String,您好,我的2d数组列正在打印两次。请帮我识别错误的代码。以下是我尝试过的: public class ArrayExercise { public static void main(String[] args){ String[][] myArray = { {"Philippines", "South Korea", "Japan", "Israel"}, // Countries {"Manila", "Seoul", "Tokyo", "Jeru

您好,我的2d数组列正在打印两次。请帮我识别错误的代码。以下是我尝试过的:

public class ArrayExercise {
public static void main(String[] args){


    String[][] myArray = {
        {"Philippines", "South Korea", "Japan", "Israel"}, // Countries
        {"Manila", "Seoul", "Tokyo", "Jerusalem" } // capital cities
    };

    String outputString = String.format("%16s\t%9s", "Country", "City" );
    System.out.println(outputString);

    for( int col = 0; col < myArray[0].length; ++col ){
        for( int row = 0; row < myArray.length; ++row ){  
           System.out.printf( "%16s\t%9s", myArray[0][col], myArray[1][col]  );
        }         
        System.out.println();         
    }
  }

}
公共类数组执行{
公共静态void main(字符串[]args){
字符串[][]myArray={
{“菲律宾”、“韩国”、“日本”、“以色列”},//国家
{“马尼拉”、“首尔”、“东京”、“耶路撒冷”}//首都城市
};
String outputString=String.format(“%16s\t%9s”、“国家”、“城市”);
System.out.println(outputString);
对于(int col=0;col

这让我发疯,我似乎找不到错误:(

删除内部循环,因为您实际上不需要它来完成您试图完成的任务:

for ( int col = 0; col < myArray[0].length; ++col ) {
  System.out.printf( "%16s\t%9s", myArray[0][col], myArray[1][col]);
  System.out.println();
}
for(int col=0;col

也就是说,对于每个国家(
myArray[0][col]
),打印其首都(
myArray[1][col]
)。

内部循环使其打印两次。您从不使用

for( int col = 0; col < myArray[0].length; ++col ){
    System.out.printf( "%16s\t%9s", myArray[0][col], myArray[1][col]  );
    System.out.println();         
}
for(int col=0;col
在内部循环中,您正在打印两行:-
myArray[0][col],myArray[1][col]

然后,您将使用内部循环迭代该内容两次:-

    for( int row = 0; row < myArray.length; ++row ){  
       System.out.printf( "%16s\t%9s", myArray[0][col], myArray[1][col]  );
    } 
for(int row=0;row
您需要删除此内部循环:-

for( int col = 0; col < myArray[0].length; ++col ){

    System.out.printf( "%16s\t%9s", myArray[0][col], myArray[1][col]  );

    System.out.println();         
}
for(int col=0;col
打印同一内容时,代码循环两次。请移除内部for循环。

使用以下方法:

System.out.printf( "%16s\t", myArray[row][col]  );
而不是:

System.out.printf( "%16s\t%9s", myArray[0][col], myArray[1][col]  );
您正在打印这两行:-
myArray[0][col],myArray[1][col]

您的“myArray.length”是两个。因此,代码两次通过内部“for”循环。循环变量“row”从未在内部“for”中使用。因此,相同的内容会打印两次


取出内部的“for”。

你有一个for循环太多了,我删除了最里面的一个,它按预期工作

public static void main(String[] args){


    String[][] myArray = {
        {"Philippines", "South Korea", "Japan", "Israel"}, // Countries
        {"Manila", "Seoul", "Tokyo", "Jerusalem" } // capital cities
    };

    String outputString = String.format("%16s\t%9s", "Country", "City" );
    System.out.println(outputString);

    for( int col = 0; col < myArray[0].length; col++ ){
        System.out.printf( "%16s\t%9s", myArray[0][col], myArray[1][col]  );
        System.out.println();         
    }
}  
publicstaticvoidmain(字符串[]args){
字符串[][]myArray={
{“菲律宾”、“韩国”、“日本”、“以色列”},//国家
{“马尼拉”、“首尔”、“东京”、“耶路撒冷”}//首都城市
};
String outputString=String.format(“%16s\t%9s”、“国家”、“城市”);
System.out.println(outputString);
for(int col=0;col
考虑你的数组不像一个棋盘,你必须访问每一行的每一列才能访问每一个方块,而更像一个橱柜

你知道有多少列。(两列)


因此,您只需在第一列中循环输入国家的数量,并在第二列中检查每个条目的大写字母。

删除内部for循环…您刚才问了这个问题??我认为您的疑问很清楚。。再次感谢@RohitJain!