Javaのコードです public class Main { public static void main(String[] args) {
Javaのコードです public class Main { public static void main(String[] args) { int [] [] scores = {{40,50,60},{80,60,70}}; System.out.println(scores.length); System.out.println(scores[0].length); } } 実行結果がなぜ2と3なんのか教えていただけますでしょうか?
Java・45閲覧
ベストアンサー
int[][]scores = {{40,50,60},{80,60,70}}; は、以下のコードと同義。 int[] score0 = {40, 50, 60}; int[] score1 = {80, 60, 70}; int[][] scores = {score0, score1}; System.out.println(scores.length); ↓ 配列scoresの要素数を出力する。 scoresの中身は{score0, score1}だから、要素は2個。 よって、2が出力される。 System.out.println(scores[0].length); ↓ 配列scoresの添字0に格納されている配列の要素数を出力する。 scoresの中身は{score0, score1}だから、scores[0]はscore0を指す。 score0の中身は{40, 50, 60}だから、要素は3個。 よって、3が出力される。
質問者からのお礼コメント
わかりやすい解答ありがとうございました!
お礼日時:7/5 0:26