Top Banner
Lecture 05 문자열, 배열, 디버깅 Kwang-Man Ko [email protected] , compiler.sangji.ac.kr Department of Computer Engineering Sang Ji University 2018
32

문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, [email protected] 2 문자열(string)

Oct 09, 2019

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

Lecture 05

문자열, 배열, 디버깅

Kwang-Man Ko

[email protected], compiler.sangji.ac.kr

Department of Computer Engineering

Sang Ji University

2018

Page 2: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

2Lecture 05: 문자, 배열, 디버깅, [email protected]

문자열 (string)

n문자열의 선언과 생성

l문자열 리터럴은 내부적으로 new String()을 호출해 생성한 객체

ls1은 new String(“안녕, 자바!”)를 호출해서 생성한 객체

l내용이 같은 문자열 리터럴이라면 더 이상 새로운 String 객체를 생성하지 않은 채 기존 리터털을 공유. 따라서 s1과 s2는 동일한 String 객체

Page 3: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

3Lecture 05: 문자, 배열, 디버깅, [email protected]

문자열

n문자열의 비교l==와 != 연산자는 두 문자열의 내용을 비교하는 것이 아니라 동일한

객체인지 검사

l예제 : sec01/String1Demo.java

Page 4: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

4Lecture 05: 문자, 배열, 디버깅, [email protected]

문자열

n문자열 비교

lString 클래스에서 제공하는 문자열 비교 메서드

l예제 : sec01/String2Demo.java

Page 5: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

5Lecture 05: 문자, 배열, 디버깅, [email protected]

문자열

n문자열 조작 : String 클래스에서 제공하는 메서드

Page 6: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

6Lecture 05: 문자, 배열, 디버깅, [email protected]

문자열

n문자열의 조작l예제 : sec01/String3Demo.java

l예제 : sec01/String4Demo.java

Page 7: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

7Lecture 05: 문자, 배열, 디버깅, [email protected]

배열 (array)

n배열이란

Page 8: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

8Lecture 05: 문자, 배열, 디버깅, [email protected]

배열

n배열의 필요성

Page 9: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

9Lecture 05: 문자, 배열, 디버깅, [email protected]

배열 기초

n배열의 선언과 생성l배열의 선언 : 실제는 배열 변수의 선언

l배열의 선언과 생성 : 실제는 배열 변수의 선언과 초기화

Page 10: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

10Lecture 05: 문자, 배열, 디버깅, [email protected]

배열

n배열의 선언과 생성

Page 11: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

11Lecture 05: 문자, 배열, 디버깅, [email protected]

배열

n배열 원소의 접근

n배열의 크기l배열이 생성될 때 배열의 크기가 결정

l배열의 length 필드가 배열의 크기를 나타냄.

lscores가 가리키는 배열의 크기는 scores.length

n예제 : sec02/Array1Demo.java

Page 12: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

12Lecture 05: 문자, 배열, 디버깅, [email protected]

배열 기초

n다차원 배열 (multi-demension array)l배열의 배열

l학생 3명의 5과목 성적을 처리하는 정수 타입 2차원 배열(3행 × 5열)인scores를 선언하고 생성.

Page 13: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

13Lecture 05: 문자, 배열, 디버깅, [email protected]

배열

n다차원 배열l선언과 초기화

l예제 : sec02/Array2Demo.java

Page 14: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

14Lecture 05: 문자, 배열, 디버깅, [email protected]

배열

n동적 배열 (static array)l처리할 데이터의 개수가 고정된 경우가 아니라면 정적 배열은 자원을

낭비하거나 프로그램을 다시 컴파일

l자바는 크기가 유동적인 배열을 지원하기 위하여 ArrayList 클래스를 제공

Page 15: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

15Lecture 05: 문자, 배열, 디버깅, [email protected]

배열 기초

n동적 배열lArrayList 객체 생성

lArrayList 원소 접근

l예제 : sec02/ArrayListDemo.java

기초 타입이라면 Integer, Long, Short, Float, Double 등을사용한다.

Page 16: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

16Lecture 05: 문자, 배열, 디버깅, [email protected]

배열 응용

n배열을 위한 반복문lfor~each 반복문 : JDK 5부터 도입된 것으로 for 문을 개선한 방식. 특정

원소를 나타내기 위한 인덱스를 사용하지 않음.

l예제 : sec03/ForEachDemo.java

Page 17: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

17Lecture 05: 문자, 배열, 디버깅, [email protected]

배열 응용

n메서드의 인수로 배열 전달l예제 : sec03/IncrementDemo.java

Page 18: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

18Lecture 05: 문자, 배열, 디버깅, [email protected]

배열 응용

n메인 메서드의 매개변수 전달l명령창에서의 실행 명령

l예제 : sec03/MainArgumentDemo.java

Page 19: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

19Lecture 05: 문자, 배열, 디버깅, [email protected]

배열 응용

n메인 메서드의 매개변수 전달l이클립스에서 매개변수 제공

Page 20: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

20Lecture 05: 문자, 배열, 디버깅, [email protected]

배열 응용

n가변 개수 인수lJDK 5부터는 메서드에도 데이터 타입이 같은 가변 개수(variable

length)의 인수를 전달 가능

l한 개의 가변 개수 매개변수만 사용 가능하며 가변 개수 매개변수는 마지막에 위치

l가변 개수 인수를 가진 메서드를 호출하면 내부적으로 배열을 생성하여 처리

l예제 : sec03/VarArgsDemo.java

Page 21: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

21Lecture 05: 문자, 배열, 디버깅, [email protected]

배열 응용

n객체의 배열l객체 배열은 객체를 참조하는 주소를 원소로 구성

lBall 클래스의 객체로 구성된 배열을 선언하고 초기화

l생성자를 호출하여 Ball 객체를 생성해야 함

5개의 Ball 객체를 생성하는 것이 아니라5개의 Ball 객체를 참조할 변수를 준비

Page 22: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

22Lecture 05: 문자, 배열, 디버깅, [email protected]

배열 응용

n객체의 배열l예제 : sec03/CircleArrayDemo.java

Page 23: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

23Lecture 05: 문자, 배열, 디버깅, [email protected]

배열 응용

n매개변수로 객체 전달l예제 : sec03/ObjectArgumentDemo.java

Page 24: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

24Lecture 05: 문자, 배열, 디버깅, [email protected]

디버깅

n오류의 종류l변수 n2에는 n2, 변수 n3에는 n3, 변수 m에는 n/d을 대입하는 예제

Page 25: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

25Lecture 05: 문자, 배열, 디버깅, [email protected]

디버깅

n이클립스를 이용한 디버깅 과정l이클립스는 에디터 뷰에 문법 오류는 알려주지만 논리 오류는 알려주

지 않음

l논리 오류는 프로그램 실행 도중에 변수 상태를 추적하는 것이 가장 기본적인 오류 점검 방식

l디버그 퍼스펙티브로 이동

Page 26: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

26Lecture 05: 문자, 배열, 디버깅, [email protected]

디버깅

n디버깅 명령어와 이클립스 단축키

Page 27: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

27Lecture 05: 문자, 배열, 디버깅, [email protected]

디버깅

n샘플 프로그램

Page 28: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

28Lecture 05: 문자, 배열, 디버깅, [email protected]

디버깅

n디버그 퍼스펙티브와 중단점 설정

Page 29: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

29Lecture 05: 문자, 배열, 디버깅, [email protected]

디버깅

n디버그 퍼스펙티브와 각종 실행 버튼

Page 30: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

30Lecture 05: 문자, 배열, 디버깅, [email protected]

디버깅

n디버그 퍼스펙티브와 변수 추적

Page 31: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

31Lecture 05: 문자, 배열, 디버깅, [email protected]

디버깅

n디버그 퍼스펙티브와 실행 결과

Page 32: 문자열 배열 디버깅 - compiler.sangji.ac.krcompiler.sangji.ac.kr/lecture/java/2018/lecture05.pdf · Lecture 05: 문자,배열,디버깅, kkman@sangji.ac.kr 2 문자열(string)

Q & A