【发布时间】:2018-02-21 21:13:41
【问题描述】:
我无法让我的二维数组并排打印取第一个数组行值索引 0 并将其匹配到第二行数组索引 0 并在垂直列表中继续打印的值。就目前而言,我的代码编译并仅在两行上以水平方式打印整个数组。基本上,我希望最终结果的格式如下:
Admin | Password1
Vale.Vicky | BruceIsTheBat!
Lane.Lois | FlyMeToTheMoon1234
Kent.Clark | PhoneBoothsSmell
Wayne.Bruce | ThisBat4You99
...等等。
public class PasswordArrays { // start of class
public static void main(String[] args) { // Start of main
// TODO Auto-generated method stub
String [][] idArray = {
{"Admin", "Vale.Vicky", "Lane.Lois", "Kent.Clark", "Wayne.Bruce", "Parker.Peter", "Rogers.Steve", "Luther.Lex", "Osborn.Harry","Prince.Diana", "Linda Zoel"},
{"Password1", "BruceIsTheBat!", "FlyMeToTheMoon1234", "PhoneBoothsSmell","ThisBat4You99", "webSlinger","ShieldEnthusiast", "HairClub4Men", "GoblinGoober", "WonderWonderWho?", "WhoIsLindaZoel?"}
};
printArray(idArray);
} //End of main
public static void printArray(String a [][]) { //start of printArray method
for (int row=0; row < a.length ; row++) { // start of row for loop
for (int column = 0; column < a [row].length; column++) { //start of column for loop
System.out.print(a[row][column] + " ");
} // End of column for loop
System.out.println();
} // End of row for loop
} // End of printArray method
} //End of class
我知道在某个地方已经有一个答案,但我一直没有找到它。非常感谢任何帮助。
【问题讨论】:
-
看来你需要学习使用调试器了。请帮助自己一些complementary debugging techniques。如果您之后仍有问题,请随时回来提出更具体的问题。
-
IntStream.range(0, idArray[0].length).map(i -> idArray[0][i] + " | " + idArray[1][i]).forEach(System.out::println);
标签: java arrays string printing formatting