728x90
변수 선언: 타입[] 변수명
int[] myNum = {10, 20, 30, 40};
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars[0]); // Outputs Volvo
cars[0] = "Opel";
System.out.println(cars[0]); // Now outputs Opel instead of Volvo
System.out.println(cars.length); // Outputs 4
배열과 for문
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
// 1번
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
}
// 2번
for (String i : cars) {
System.out.println(i);
}
다차원 배열
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
System.out.println(myNumbers[1][2]); // Outputs 7
// -------------------------
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
myNumbers[1][2] = 9;
System.out.println(myNumbers[1][2]); // Outputs 9 instead of 7
// -------------------------
public class Main {
public static void main(String[] args) {
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
for (int i = 0; i < myNumbers.length; ++i) {
for(int j = 0; j < myNumbers[i].length; ++j) {
System.out.println(myNumbers[i][j]);
}
}
}
}
728x90
댓글