java 配列のサイズを取得する

javaで、配列のサイズを取得する手順を記述してます。
環境
- OS windows11 home
- java 17.0.2
手順
配列のサイズを取得するには、「length」を使用します。
配列名.length
実際に、使用してみます。
public class App {
public static void main(String[] args) throws Exception {
int[] num = new int[2];
System.out.println(num.length); // 2
}
}
取得されていることが確認できます。
二次元配列
二次元配列の場合は、インデックス番号を指定して取得します。
public class App {
public static void main(String[] args) throws Exception {
int[][] num = new int[2][5];
System.out.println(num[0].length); // 5
System.out.println(num[1].length); // 5
}
}
-
前の記事
MySQLのエラー『Incorrect Key File for Table』の解決方法 2025.05.24
-
次の記事
kotlin mutableMapの値のみを全て取得する 2025.05.26
コメントを書く