2017-11-05 9 views
-2

私は質問にコメントしました!私はそれが最初にBMW、そしてHyundaiと最後のDodgeを言いたい。反復するたびに配列を変更するにはどうすればよいですか?

sa.append ("The manufacturer " + cars [0] [0]);

へ:

String[] [] cars = new String[3] [2]; 
    cars [0] [0] = "BMW"; 
    cars [0] [1] = "i7!"; 
    cars [1] [0] = "Hyundai"; 
    cars [1] [1] = "Tuscon!"; 
    cars [2] [0] = "Dodge"; 
    cars [2] [1] = "Challenger!"; 

    for (int i = 0; i < cars.length; i++) { 
     StringBuilder sa = new StringBuilder(); 
     for (int j = 0; j < cars[i].length; j++) { 
      if (j == 0) { 
       // what can I do to make the final statement (the name of manufacturer change for each time it is printed out, instead of i.e BMW for all of em 
       sa.append ("The manufacturer " + cars1 [0] [0]); sa.append (" has just published a video of "); 
      } else { 
       sa.append(" the model "); 
      } 
      sa.append(cars [i] [j]); 
     } 

     System.out.println(sa); 
    } 
} 

}

+3

変更 'cars1 [0] [0]'へ 'cars1 [i] [j] '? – Eran

+4

車に関する情報を格納するために配列を使用しないでください。フィールドに「manufacturer」フィールドと「model」フィールドがあるCarクラスを定義します。次に、車のアレイを作成し、車のアレイを繰り返します。 JavaはOO言語です。あなたはオブジェクトを使うはずです。 –

+0

'Collections.shuffle(Arrays.asList(cars))'。これは配列の最初の次元でのみシャッフルされるので、製造元とモデルは一緒に保持されます。 –

答えて

0

あなたが唯一変更する必要が

sa.append ("The manufacturer " + cars [i] [j]);

+1

それは働いた、ダースありがとう。 – Farzin

+0

ようこそ。 2次元配列を使用する場合は、行iのインデックスと列jのインデックスに注意してください。 – Oghli

関連する問題