JAVA
5.Java 2차원 배열/OOP/클래스
네스이
2022. 7. 8. 17:35
2022.07.08.금
1.2차원 배열
1)정방형
import java.util.Arrays;
public class ArrayTest2_2차원_정방형 {
public static void main(String[] args)
{
//2차원 배열[행][열]
/*
2차원 정방형 배열 생성 방법 3가지
1.new 이용
int[][] n;
n = new int[3][2];
==>int[][] n = new int[3][2];
n[0][0] = 1; n[0][1] = 2; n[0][2] = 3;
n[1][0] = 4; n[1][1] = 5; n[1][2] = 6;
2.new 없이 값만 이용
int[][] n = { { 1, 2, 3 }, { 4, 5, 6 } };
==>int[][] n; n = { { 1, 2, 3 }, { 4, 5, 6 } }; 불가능
3.new + 값 이용
int[][] n = new int[][]{ { 1, 2, 3 }, { 4, 5, 6 } };
[] ==> 크기지정 불가
==> int[][] n;
n = new int[][]{ { 1, 2, 3 }, { 4, 5, 6 } };
*/
//1.new 이용 방법
int[][] n = new int[2][3];
//출력
for(int i = 0; i < n.length; i++)
{
for(int j = 0; j < n[i].length; j++)
{
System.out.println(n[i][j]);
}
}
for(int[] i : n)
{
for(int j : i)
{
System.out.println(j);
}
}
//초기화
n[0][0] = 1; n[0][1] = 2; n[0][2] = 3;
n[1][0] = 4; n[1][1] = 5; n[1][2] = 6;
System.out.println(Arrays.deepToString(n));
//2.값 이용
int[][] n2 = { { 9, 8, 7 }, { 6, 5, 4 } };
System.out.println(Arrays.deepToString(n2));
//3.new + 값 이용
int[][] n3 = new int[][]{ { 3, 2, 4 }, { 10, 32, 1 } };
System.out.println(Arrays.deepToString(n3));
//행의 길이/열의 길이
System.out.println("행의 길이: " + n3.length);
System.out.println("1행의 열 길이: " + n3[0].length);
}
}
2)비정방형
import java.util.Arrays;
public class ArrayTest2_2차원_비정방형 {
public static void main(String[] args)
{
//2차원 배열
/*
2차원 비정방형 배열 생성 방법 3가지
1.new 이용
int[][] n;
n = new int[3][];
==>int[][] n = new int[3][];
==>n[0] = new int[2]; n[1] = new int[1]; n[2] = new int[3];
n[0][0] = 1; n[0][1] = 2;
n[1][0] = 3;
n[2][0] = 4; n[2][1] = 5; n[2][2] = 6;
2.new 없이 값만 이용
int[][] n = { { 1, 2 }, { 3 }, { 4, 5, 6 } };
==>int[][] n; n = { { 1, 2 }, { 3 }, { 4, 5, 6 } }; 불가능
3.new + 값 이용
int[][] n = new int[][]{ { 1, 2 }, { 3 }, { 4, 5, 6 } };
[] ==> 크기지정 불가
==> int[][] n;
n = new int[][]{ { 1, 2 }, { 3 }, { 4, 5, 6 } };;
*/
//1.new 이용 방법
int[][] n = new int[3][];
n[0] = new int[2]; n[1] = new int[1]; n[2] = new int[3];
//출력
for(int i = 0; i < n.length; i++)
{
for(int j = 0; j < n[i].length; j++)
{
System.out.println(n[i][j]);
}
}
for(int[] i : n)
{
for(int j : i)
{
System.out.println(j);
}
}
//초기화
n[0][0] = 1; n[0][1] = 2;
n[1][0] = 3;
n[2][0] = 4; n[2][1] = 5; n[2][2] = 6;
System.out.println(Arrays.deepToString(n));
//2.값 이용
int[][] n2 = { { 1, 2 }, { 3 }, { 4, 5, 6 } };
System.out.println(Arrays.deepToString(n2));
//3.new + 값 이용
int[][] n3 = new int[][]{ { 1, 2 }, { 3 }, { 4, 5, 6 } };
System.out.println(Arrays.deepToString(n3));
//행의 길이/열의 길이
System.out.println("행의 길이: " + n3.length);
System.out.println("1행의 열 길이: " + n3[0].length);
}
}
2.OOP(Object Oriented Programing, 객체 지향 프로그래밍)
1)의미 : 객체를 이용해서 프로그래밍하는 것
2)객체(object)
가.의미 : 우리(주체, subject) 눈에 보이는 물건(사물)
예) 모니터, 책상, 의자, 강사, 학생 etc.
나.객체의 구성요소
-속성 : 객체 정보 예)크기, 가격, 색상, 제조사, 이름, 나이
-구성 : 객체 행위(동작) 예)on/off, study, walk
3)자바프로그램 개발 프로세스
고객 의뢰 -> 분석(객체 추출) -> 설계(자바 클래스) -> 구현 -> 테스트 -> 배포
예) 학생관리 프로그램
분석(객체 추출) : 학생객체(속성, 동작), 교사객체(속성, 동작), 객체간 관계
설계 : 학생 객체(Student Class), {학생 객체 속성(변수), 학생 객체 동작(메서드)}
4)OOP 3대 특징
-상속(inheritance)
-다형성(polymorphsim)
-은닉화( encapsulation)
3.Java 클래스
1)인스턴스 변수
//객체(object)
//재사용 클래스
//혼자서는 실행불가, 다른 클래스(main이 있는 클래스) 도움(객체 생성/new)으로 로딩가능
/*
예)학생관리 프로그램 개발
1)분석단계 2)설계단계
->학생 객체 추출 ================> 학생 클래스(Student)/main없이 작성
-속성 : 이름, 나이, 주소 =======> 인스턴스 변수(객체의 속성 저장)
-동작 : ======> 메서드
*/
public class Student
{
//인스턴스 변수(객체의 속성) : 객체 생성(new)시 생성됨(heap 영역)
String name;
String address;
int age;
}
public class TestStudent
{
public static void main(String[] args)
{
//학생정보 저장 => 홍길동, 서울, 20
//Student 클래스 이용하여 저장
//Student클래스는 main메서드 없기 때문에 독자적 메모리 로딩 불가
//main메서드에서 Student클래스 메모리 로딩 가능
//위의 작업을 객체 생성이라고 함
//객체생성 문법 : 클래스명 변수명 = new 클래스명();
//객체가 생성되서 heap메모리에 로딩되면 instance가 됨
Student s = new Student();
//인스턴스 변수 접근 : 클래스변수명.인스턴스변수명
s.name = "홍길동";
s.address = "서울";
s.age = 20;
//출력
System.out.println(s.name);
System.out.println(s.address);
System.out.println(s.age);
}
}
2)생성자
/*
1.클래스
-클래스가 자바 프로그램의 최소단위
-클래스명 : 사용자 정의 식별자
권장 : 첫글자 대문자 명사형 지정(명명법 : naming convention)
-문법 : public class 클래스명 {
//3가지 구성요소
1)변수(인스턴스 변수, static 변수)
-데이터 저장
2)메서드(method)
-변수값 변경, 조회
3)생성자(constructor)
-변수 초기화
}
2.생성자(constructor)
-메서드처럼 호출해야 종료시 호출한 곳으로 돌아옴(method-like)
-new 할 때 호출해서 수행
-문법 : public 클래스명() {
}
클래스명 변수 = new 클래스명();
//생성자 호출 코드
-클래스에 생성자가 명시적으로 없으면 자동으로 생성(기본/default 생성자)
-명시적으로 생성자가 있으면 기본 생성자 제공안됨
-오버로딩(overloading) 생성자 : 인자 리스트가 다른 생성자
예) public Student() {}
public Student(int n) {}
public Student(String n) {}
public Student(int n, String x) {}
*/
public class Student
{
//변수
String name;
String address;
int age;
//기본 생성자
//이클립스 자동 생성자 호출 커맨드(오른쪽 클릭 -> Source -> Generate Constructor)
public Student() {}
//오버로딩 생성자 //파라미터 변수(로컬 변수)
public Student(String na, String ad, int ag)
{
name = na;
address = ad;
age = ag;
}
public Student(String na)
{
name = na;
}
}
public class TestStudent
{
public static void main(String[] args)
{
//홍길동, 20, 서울
// Caller와 Callee의 인자 리스트가 일치해야 함(갯수, 타입, 순서)
Student s = new Student("홍길동", "서울", 20);
System.out.println(s.name);
System.out.println(s.address);
System.out.println(s.age);
Student s2 = new Student(); //기본생성자 만들면 생성 가능
}
}