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

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Post

  • 8 things you need to think about for work-life balance
    View on Youtube Image: juliezhuo.com who works as a design VP for Facebook, presented 8 ways for work and life balance in her article “The
  • 10 special abilities of Smartphone
    View on Youtube No matter where you go or what you do, your smartphone is always with us. We use the internet with a smartphone,
  • Java: abstract class, abstract method
    An abstract class in Java is a class that declares an abstract function to be implemented in a subclass as an abstract method. An abstract
  • Java: inheritance and constructors
    When an object is created from an inherited class, the constructor of the superclass is performed before the constructor of the subclass. That is, when
  • Java reserved words: super, final
    super is a Java reserved word that is used to access member variables or methods of a superclass that is obscured by a subclass. Access
  • Java: finalize method and garbage collection
    The Java Virtual Machine(JVM) automatically performs garbage collection for objects that are no longer needed for efficient system operation. At this point, the JVM calls
  • Java: Method overloading
    Method overloading is the same concept as constructor overloading. That is, you can use methods of the same name in the same class, but methods
  • Java final variable
    You can specify an end variable using the reserved word final. The final variable represents a constant value that can not be changed. Typically, final
  • Java class variables
    Class variables are declared with static and have the concept of global variables. Class variables are used for the following purposes. Object references and object
  • Member variables in Java
    Java’s member variables are all declared outside the methods in the class and represent the properties that the object can have. Member variables consist of