2차원 배열이란 배열의 배열
타입 배열이름[열의길이][행의길이];
int num[n][m];
// 2차원 배열
// 2차원 배열 선언
int[][] test;
// 2차원 배열 생성
test = new int[2][3];
// 행번호 열번호
test[0][0] = 100;
test[0][1] = 200;
test[0][2] = 300;
test[1][0] = 400;
test[1][1] = 500;
test[1][2] = 600;
System.out.println(test[0][0]);
System.out.println(test[0][1]);
System.out.println(test[0][2]);
System.out.println(test[1][0]);
System.out.println(test[1][1]);
System.out.println(test[1][2]);
System.out.println("-------------------------");
//행의 길이
for(int i=0; i<test.length; i++) {
for(int j=0; j<test[i].length; j++) {
System.out.println("test["+i+"]["+j+"] = "+test[i][j]);
}
}
//2차원 배열의 선언 및 생성
int[][] test2 = new int[3][4]; //3행 4열
//2차원 배열의 선언 및 생성(암시적 배열 생성), 초기화
int[][] test4 = {
{10,20,30,40},
{50,60,70,80},
{90,10,20,30}
};
100
200
300
400
500
600
-------------------------
test[0][0] = 100
test[0][1] = 200
test[0][2] = 300
test[1][0] = 400
test[1][1] = 500
test[1][2] = 600
클래스 (Class) : 건물을 지을 때 제일 먼저 필요로 하는 설계도면
객체 (Object) : 설계도면을 통해 완성된 건물
public class test{
int a; //변수 필드
String s//문자열
public void hell0(){ // 메서드
System.out.println("Hello");
}
예제
class Car{
String company;
String model;
String color;
int maxSpeed;
int speed;
}
public class CarMain {
public static void main(String[] args) {
//객체 선언 및 생성
Car myCar = new Car();
//객체의 필드에 저장된 값 출력
System.out.println("제작회사 : "+myCar.company);
System.out.println("모델명 : "+myCar.model);
System.out.println("색깔 : "+myCar.color);
System.out.println("최고 속도 : "+myCar.maxSpeed);
System.out.println("현재 속도 : "+myCar.speed);
//객체의 필드에 값 저장
myCar.company="햔대자동차";
myCar.model="소나타";
myCar.color="검정";
myCar.maxSpeed=500;
myCar.speed=300;
System.out.println("제작회사 : "+myCar.company);
System.out.println("모델명 : "+myCar.model);
System.out.println("색깔 : "+myCar.color);
System.out.println("최고 속도 : "+myCar.maxSpeed);
System.out.println("현재 속도 : "+myCar.speed);
}
}
결과 값
제작회사 : null
모델명 : null
색깔 : null
최고 속도 : 0
현재 속도 : 0
제작회사 : 햔대자동차
모델명 : 소나타
색깔 : 검정
최고 속도 : 500
현재 속도 : 300