-2

Javaで特定の方法でArrayListの内容を印刷するにはどうすればよいですか? 例えば:

System.out.println("A        B");
System.out.println("100     100");
System.out.println("200     200");
System.out.println("300     300");

ArrayList でこれを行うにはどうすればよいですか?

配列リストを次のように書きます。

ArrayList mathChoices = new ArrayList(3); 
mathChoices.add("100");
mathChoices.add("200");
mathChoices.add("300");

ArrayList historyChoices = new ArrayList(3);
historyChoices.add("100");
historyChoices.add("200");
historyChoices.add("300");

そして、次のように出力されます。

Math        History
[100, 200, 300] [100, 200, 300]

「MATH」、次に「HISTORY」を各単語の下に 100,200,300 の垂直列で出力したいと考えています。これは、ゲーム ショー Jeopardy の質問のようなものであるはずです。

4

1 に答える 1

1

コメントに同意するので、更新された回答を次に示します。

System.out.println(String.format("%-20s %s" , "Mathematics", "History" ));

for (int i = 0; i<3; i++)
{
    System.out.println(String.format("%-20s %s" , mathChoices.get(i), historyChoices.get(i)));    
}

出力は要求どおりになります。

于 2013-08-23T00:41:45.533 に答える