【JAVA】String[]配列の相関

时间:2021-09-14 02:19:35

配列の作成:

 ①String[] str = new String[5];

 ②String[] str = new String[]{"","","","",""};

 ③String[] str = {"","","","",""};

 ④String[][] str = new String[2][2];

 ⑤String[] str = new String[]{};

配列の値の取得:

 //普通の方法
String[][] str = new String[][]{{"a","b","c"},{"d","e","f"},{"g","h","i"}};
for(int i=0;i<str.length;i++){
for(int j=0;j<str[i].length;j++){
System.out.println("str["+i+"]["+j+"]:"+str[i][j]);
}
}
//方法一(推薦)
String[][] str = new String[][]{{"a","b","c"},{"d","e","f"},{"g","h","i"}};
for(String[] row : str){
for(String s : row){
System.out.println(s);
}
}
//方法二(推薦)
String[][] str = new String[][]{{"a","b","c"},{"d","e","f"},{"g","h","i"}};
System.out.println(Arrays.deepToString(str));