Java array and 6 examples

An array is a data structure for storing the same kind of data. In Java, arrays are treated as objects, and the size of arrays created can not change.

One-dimensional array declaration

type arrayName[];
type[] arrayName;

Multidimensional array declaration

type arrayName[][];
type[][] arrayName;

For multidimensional arrays, the part that creates an array is called a index. The front index is called left index and the back index is called right index.

Creating an array

arrayName = new type[size];
ex) int two_dim[][] = new int[4][5];

1. Basic Array Example 1

class Array {
	public static void main(String args[]) {
		String weekly-days[] = new String[7];
		weekly_days[0] = "월요일";
		weekly_days[1] = "화요일";
		weekly_days[2] = "수요일";
		weekly_days[3] = "목요일";
		weekly_days[4] = "금요일";
		weekly_days[5] = "토요일";
		weekly_days[6] = "일요일";

		System.out.println("My favorite day is " + weekly_days[5]);
	}
}
My favorite day is Saturday
Result

2. Basic array example 2

class AutoArray {
	public static void main(String args[]) {
		String weeklyDays[] = {"월요일", " 화요일", "수요일", "목요일", "금요일", "토요일", "일요일"};
		System.out.println("My favorite day is " + weeklyDays[5]);
	}
}
My favorite day is Saturday
Result

3. Multidimensional Array Example 1

class TwoArray {
	public static void main(String args[]) {
		int two_array[][] = new int[4][5];
		int i, j, k = 0;

		for(i = 0; i<4; i++) {
			for(j = 0; j<5; j++) {
				two_array[i][j] = k;
				k++;
			}
		}

		for(i = 0; i<4; i++) {
			for(j = 0; j<5; j++) {
				 System.out.print(two_array[i][j] + "");
			}
			System.out.println();
		}
	}
}
01234
56789
1011121314
1516171819
Result

4. Multidimensional Array Example 2

class TwoDA {
	public static void main(String args[]) {
		int twoD[][] = new int[4][];
		twoD[0] = new int[1];
		twoD[1] = new int[2];
		twoD[2] = new int[3];
		twoD[3] = new int[4];

		int i, j, k = 0;

		for(i = 0; i<4; i++) {
			for(j = 0; j<i+1; j++) {
				twoD[i][j] = k;
				k++;
			}
		}

		for(i = 0; i<4; i++) {
			for(j = 0; j<i+1; j++) {
				System.out.print(twoD[i][j] + " ");
			}
			System.out.println();
		}
	}
}
0
1 2
3 4 5
6 7 8 9
Result

5. Multidimensional array example 3

class TwoDA1 {
	public static void main(String args[]) {
		int twoDarray[][] = {
			{12, 34, 56},
			{23, 45, 67, 89},
			{123, 456}
		};

		for(int i = 0; i<3; i++) {
			System.out.println("The length of the twoDarray [" + i + "] column is " + twoDarray[i].length);
		}
	}
}
The length of the twoDarray [0] column is 3
The length of the twoDarray [1] column is 4
The length of the twoDarray [2] column is 2
Result

6. Multidimensional Array Example 4

class ThreeDA {
	public static void main(String args[]) {
		int threeD[][][] = new int[3][4][5];
		int i, j, k;

		for(i = 0; i<3; i++) {
			for(j = 0; j<4; j++) {
				for(k = 0; k<5; k++) {
					threeD[i][j][k] = i;
				}
			}
		}

		for(i = 0; i<3; i++) {
			System.out.println((i+1) + "two-dimensional array ");
			for(j = 0; j<4; j++) {
				for(k = 0; k<5; k++) {
					System.out.print(threeD[i][j][k] + " ");
				}
				System.out.println();
			}
			System.out.println();
		}
	}
}
1 two-dimensional array
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

2 two-dimensional array
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

3 two-dimensional array
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
Result

Share This Post

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다

Latest Post