简体   繁体   中英

Java 2d array column is printing twice

Hello my 2d array column is printing twice. 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]

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] ).

The inner loop makes it print twice. You never use 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.

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]

Your "myArray.length" is two. Thus the code passes through the inner "for" loop twice. The loop variable "row" is never used inside the inner "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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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