多次元配列
| 日本語 | 多次元配列 |
| 英語 | multidimensional array |
| ふりがな | たじげんはいれつ |
| フリガナ | タジゲンハイレツ |
配列の中に配列が入っており、さらにその中に配列が入っている、そんな配列。
二次元配列のさらに次元数が多い配列を指す。
基本的には二次元配列と同じく「配列の中に配列が入っている」のが繰り返されているだけである。
アクセス方法も「[]」をそのまま増やしていけば同様に扱える。
ただ、実際にはややこしいこと極まりなく、文法的にも難しい面があるため、素直にArrayListを使用した方がいいとも言える。
二次元配列のさらに次元数が多い配列を指す。
基本的には二次元配列と同じく「配列の中に配列が入っている」のが繰り返されているだけである。
アクセス方法も「[]」をそのまま増やしていけば同様に扱える。
ただ、実際にはややこしいこと極まりなく、文法的にも難しい面があるため、素直にArrayListを使用した方がいいとも言える。
参考サイト
- (参考サイトはありません)
// Sample.java
public class Sample
{
public static void main( String[] args )
{
// int型の三次元配列を作ります。
int[][][] intsOfIntsOfInts
= new int[][][]
{
new int[][]
{
new int[]{ 10, 20, 30 }
, new int[]{ 100, 200, 300 }
},
new int[][]
{
new int[]{ 1000, 2000, 3000 }
, new int[]{ 10000, 20000, 30000 }
}
};
// このように「『〈intの配列〉を入れる配列』を入れる配列」
// を作っています。
// 左側の添え字が「『〈intの配列〉を入れる配列』を入れる配列」、
// 中央の添え字が『〈intの配列〉を入れる配列』、
// 右側の添え字が〈intの配列〉になります。
System.out.println( intsOfIntsOfInts[0][0][0] );
System.out.println( intsOfIntsOfInts[0][0][1] );
System.out.println( intsOfIntsOfInts[0][0][2] );
System.out.println( intsOfIntsOfInts[0][1][0] );
System.out.println( intsOfIntsOfInts[0][1][1] );
System.out.println( intsOfIntsOfInts[0][1][2] );
System.out.println( intsOfIntsOfInts[1][0][0] );
System.out.println( intsOfIntsOfInts[1][0][1] );
System.out.println( intsOfIntsOfInts[1][0][2] );
System.out.println( intsOfIntsOfInts[1][1][0] );
System.out.println( intsOfIntsOfInts[1][1][1] );
System.out.println( intsOfIntsOfInts[1][1][2] );
// 10
// 20
// 30
// 100
// 200
// 300
// 1000
// 2000
// 3000
// 10000
// 20000
// 30000
// 『〈intの配列〉を入れる配列』だけ取り出すこともできます。
int[][] intsOfInts = intsOfIntsOfInts[0];
System.out.println( intsOfInts[0][0] );
System.out.println( intsOfInts[0][1] );
System.out.println( intsOfInts[0][2] );
System.out.println( intsOfInts[1][0] );
System.out.println( intsOfInts[1][1] );
System.out.println( intsOfInts[1][2] );
// 10
// 20
// 30
// 100
// 200
// 300
}
}
public class Sample
{
public static void main( String[] args )
{
// int型の三次元配列を作ります。
int[][][] intsOfIntsOfInts
= new int[][][]
{
new int[][]
{
new int[]{ 10, 20, 30 }
, new int[]{ 100, 200, 300 }
},
new int[][]
{
new int[]{ 1000, 2000, 3000 }
, new int[]{ 10000, 20000, 30000 }
}
};
// このように「『〈intの配列〉を入れる配列』を入れる配列」
// を作っています。
// 左側の添え字が「『〈intの配列〉を入れる配列』を入れる配列」、
// 中央の添え字が『〈intの配列〉を入れる配列』、
// 右側の添え字が〈intの配列〉になります。
System.out.println( intsOfIntsOfInts[0][0][0] );
System.out.println( intsOfIntsOfInts[0][0][1] );
System.out.println( intsOfIntsOfInts[0][0][2] );
System.out.println( intsOfIntsOfInts[0][1][0] );
System.out.println( intsOfIntsOfInts[0][1][1] );
System.out.println( intsOfIntsOfInts[0][1][2] );
System.out.println( intsOfIntsOfInts[1][0][0] );
System.out.println( intsOfIntsOfInts[1][0][1] );
System.out.println( intsOfIntsOfInts[1][0][2] );
System.out.println( intsOfIntsOfInts[1][1][0] );
System.out.println( intsOfIntsOfInts[1][1][1] );
System.out.println( intsOfIntsOfInts[1][1][2] );
// 10
// 20
// 30
// 100
// 200
// 300
// 1000
// 2000
// 3000
// 10000
// 20000
// 30000
// 『〈intの配列〉を入れる配列』だけ取り出すこともできます。
int[][] intsOfInts = intsOfIntsOfInts[0];
System.out.println( intsOfInts[0][0] );
System.out.println( intsOfInts[0][1] );
System.out.println( intsOfInts[0][2] );
System.out.println( intsOfInts[1][0] );
System.out.println( intsOfInts[1][1] );
System.out.println( intsOfInts[1][2] );
// 10
// 20
// 30
// 100
// 200
// 300
}
}
// Sample.java
public class Sample
{
public static void main( String[] args )
{
// int型の三次元配列を作ります。
int[][][] intsOfIntsOfInts
= new int[][][]
{
new int[][]
{
new int[]{ 10, 20, 30 }
, new int[]{ 100, 200, 300 }
},
new int[][]
{
new int[]{ 1000, 2000, 3000 }
, new int[]{ 10000, 20000, 30000 }
}
};
// このように「『〈intの配列〉を入れる配列』を入れる配列」
// を作っています。
// 左側の添え字が「『〈intの配列〉を入れる配列』を入れる配列」、
// 中央の添え字が『〈intの配列〉を入れる配列』、
// 右側の添え字が〈intの配列〉になります。
System.out.println( intsOfIntsOfInts[0][0][0] );
System.out.println( intsOfIntsOfInts[0][0][1] );
System.out.println( intsOfIntsOfInts[0][0][2] );
System.out.println( intsOfIntsOfInts[0][1][0] );
System.out.println( intsOfIntsOfInts[0][1][1] );
System.out.println( intsOfIntsOfInts[0][1][2] );
System.out.println( intsOfIntsOfInts[1][0][0] );
System.out.println( intsOfIntsOfInts[1][0][1] );
System.out.println( intsOfIntsOfInts[1][0][2] );
System.out.println( intsOfIntsOfInts[1][1][0] );
System.out.println( intsOfIntsOfInts[1][1][1] );
System.out.println( intsOfIntsOfInts[1][1][2] );
// 10
// 20
// 30
// 100
// 200
// 300
// 1000
// 2000
// 3000
// 10000
// 20000
// 30000
// 『〈intの配列〉を入れる配列』だけ取り出すこともできます。
int[][] intsOfInts = intsOfIntsOfInts[0];
System.out.println( intsOfInts[0][0] );
System.out.println( intsOfInts[0][1] );
System.out.println( intsOfInts[0][2] );
System.out.println( intsOfInts[1][0] );
System.out.println( intsOfInts[1][1] );
System.out.println( intsOfInts[1][2] );
// 10
// 20
// 30
// 100
// 200
// 300
}
}




