简体   繁体   English

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

[英]Java 2d array column is printing twice

Hello my 2d array column is printing twice. 您好我的2d数组列打印两次。 Please help me identify the erroneous code. 请帮我识别错误的代码。 Below is what I have tried: 以下是我的尝试:

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();         
    }
  }

}

It's driving me nuts, I can't seem to find the error :( 它让我疯了,我似乎无法找到错误:(

In your inner loop, you are printing both the row: - myArray[0][col], myArray[1][col] 在你的内循环中,你打印两行: - myArray[0][col], myArray[1][col]

Then you are iterating that thing twice using inner loop: - 然后你使用内循环迭代那个东西: -

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

You need to remove this inner loop: - 你需要删除这个内循环: -

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

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

    System.out.println();         
}

Remove the inner loop, since you effectively don't need it for what you are trying to accomplish: 删除内部循环,因为您实际上不需要它来完成任务:

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

That is, for each country ( myArray[0][col] ), print its capital city ( myArray[1][col] ). 也就是说,对于每个国家( myArray[0][col] ),打印其首都( myArray[1][col] )。

The inner loop makes it print twice. 内环使其打印两次。 You never use row . 你从不使用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();         
}

The code is looping twice, while printing the same thing. 代码循环两次,同时打印相同的东西。 remove the inner for loop. 删除内部for循环。

use this: 用这个:

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

instead of: 代替:

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

you are printing both the row:- myArray[0][col], myArray[1][col] 你打印的行: - myArray[0][col], myArray[1][col]

Your "myArray.length" is two. 你的“myArray.length”是两个。 Thus the code passes through the inner "for" loop twice. 因此代码两次通过内部“for”循环。 The loop variable "row" is never used inside the inner "for". 循环变量“row”从不在内部“for”中使用。 Thus the same thing is printed twice. 因此,同样的东西打印两次。

Take out the inner "for". 取出内心的“为”。

You have one for loop too many, I deleted the innermost one and it worked as expected. 你有一个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();         
    }
}  

Consider you Array not like a chess board where you have to visit every column for every row in order to visit every square, but more like a cupboard. 考虑一下阵列不像棋盘,你必须访问每一行的每一列才能访问每个方块,但更像是一个橱柜。

You know how many columns there are. 你知道有多少列。 (two) (二)

So, you only have to loop over the number of contries in your first column and check in the second column for each entry what the capital is. 因此,您只需要遍历第一列中的数量,并在每个条目的第二列中检查资本是多少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM