![](https://t1.daumcdn.net/cfile/tistory/27588E40555B064E21)
![](https://t1.daumcdn.net/cfile/tistory/2665BB40555B065118)
![](https://t1.daumcdn.net/cfile/tistory/275E8E40555B06531D)
![](https://t1.daumcdn.net/cfile/tistory/2338A540555B065536)
![](https://t1.daumcdn.net/cfile/tistory/226C3E40555B065713)
![](https://t1.daumcdn.net/cfile/tistory/2403C640555B065904)
![](https://t1.daumcdn.net/cfile/tistory/21713E40555B065C10)
![](https://t1.daumcdn.net/cfile/tistory/277D494A555B065D02)
![](https://t1.daumcdn.net/cfile/tistory/2575EC4A555B065F07)
![](https://t1.daumcdn.net/cfile/tistory/226F6B4A555B06610A)
![](https://t1.daumcdn.net/cfile/tistory/27302C4A555B066333)
![](https://t1.daumcdn.net/cfile/tistory/275DC44A555B066418)
![](https://t1.daumcdn.net/cfile/tistory/2345A24A555B066627)
![](https://t1.daumcdn.net/cfile/tistory/247DA44A555B066802)
![](https://t1.daumcdn.net/cfile/tistory/263D0E50555B066A1E)
![](https://t1.daumcdn.net/cfile/tistory/27124B50555B066D38)
![](https://t1.daumcdn.net/cfile/tistory/244CE350555B066F14)
![](https://t1.daumcdn.net/cfile/tistory/25656C50555B067102)
![](https://t1.daumcdn.net/cfile/tistory/2465AD50555B067202)
6. 자바라이브러리(API)
1
6. 자바라이브러리(API)
1
버그의 발생 버그의 발생
public String checkYourself(String …
…
if(guess == locationCells[i]) {
result = “hit”;
numOfHits++;
break;
}
…
} -한 번 맞춘 뒤에도 그 위치에만
들어오면 무조건 맞춘다고 간주
-이미 맞췄는지 확인할 방법 필요
SimpleDotCo..
enter a number 1
hit
enter a number 1
hit
enter a number 1
kill
3 guesses
한 번 맞추고 나서
같은 숫자를 계속 입력해도
게임을끝낼 수 있음
버그고치기(1/5)
셀일곱개가들어있는가상의행이있고,
DotCom 객체는연속적인셀세개를차지버그고치기(1/5)
셀일곱개가들어있는가상의행이있고,
DotCom 객체는연속적인셀세개를차지
0 1 2 3 4 5 6
DotCom 이
4, 5, 6 번
셀에
있는
것으로
가정
DotCom 에는그객체의셀위치를저장하기위한인스턴스변수가있음0 1 2
4 5 6
-locationCells (DotCom 의인스턴스변수)
-인스턴스변수는int 배열-사용자는4, 5, 6 이세개의숫자를맞춰야함
버그고치기(2/5)
배열을하나더만들고위치를맞출때마다그맞춘위치를두번째배열에집어넣음위치를맞출때마다이미맞췄는지그배열을확인함버그고치기(2/5)
배열을하나더만들고위치를맞출때마다그맞춘위치를두번째배열에집어넣음위치를맞출때마다이미맞췄는지그배열을확인함
hitCells 배엱
(DotCom 에
새로
추가핝
부욵
배엱
인스턴스
변수
)
0 1 2
false false true
-각셀의‘상태’를나타내는값세개가들어감ex) 2 번에들어있는셀을맞췄다면true로바뀜
버그고치기(3/5)
원래배열을사용하면서맞춘셀의값은모두-1 로바뀜. 배열을굳이두개사용할필요없음버그고치기(3/5)
원래배열을사용하면서맞춘셀의값은모두-1 로바뀜. 배열을굳이두개사용할필요없음
0 1 2
4 5 -1
-음수가있으면그셀을이미맞췄음을의미-배열에서음이아닌숫자만찾으면됨
locationCells
(DotCom 의
인스턴스
변수
)
버그고치기(4/5)
맞출때마다각셀위치를삭제, 배열을더작게고침배열의크기를바꿀수없기때문에새로운배열을만들고, 이전배열에남아있는셀을복사하고새로운배열로옮김버그고치기(4/5)
맞출때마다각셀위치를삭제, 배열을더작게고침배열의크기를바꿀수없기때문에새로운배열을만들고, 이전배열에남아있는셀을복사하고새로운배열로옮김
0 1 2
4 5 6
0 1
4 6
locationCells locationCells
아.
아무
셀도
맞추.
1번
인덱스에
들어
있던
않은상태
‘5’를맞춘후의상태
버그고치기(5/5)
int 배열에있는각셀에대해다음작업반복// 사용자가추측한위치를비교하는부분만약사용자가추측한것이맞으면맞춘개수를증가// 마지막위치셀인지확인만약맞춘회수가3이면“kill”을리턴그렇지않으면“hit”을리턴만약부분끝그렇지않으면“miss”를리턴만약부분끝반복부분끝기존의준비코드남아있는각위치셀에대해다음작업반복// 사용자가추측한위치를비교하는부분만약사용자가추측한것이맞으면그셀을배열에서제거// 마지막위치셀인지확인만약맞춘회수가3이면“kill”을리턴그렇지않으면“hit”을리턴만약부분끝그렇지않으면“miss”를리턴만약부분끝반복부분끝세번째옵션버그고치기(5/5)
int 배열에있는각셀에대해다음작업반복// 사용자가추측한위치를비교하는부분만약사용자가추측한것이맞으면맞춘개수를증가// 마지막위치셀인지확인만약맞춘회수가3이면“kill”을리턴그렇지않으면“hit”을리턴만약부분끝그렇지않으면“miss”를리턴만약부분끝반복부분끝기존의준비코드남아있는각위치셀에대해다음작업반복// 사용자가추측한위치를비교하는부분만약사용자가추측한것이맞으면그셀을배열에서제거// 마지막위치셀인지확인만약맞춘회수가3이면“kill”을리턴그렇지않으면“hit”을리턴만약부분끝그렇지않으면“miss”를리턴만약부분끝반복부분끝세번째옵션
Array List (1/2) Array List (1/2)
add(Object elem)
: 객체
매개변수
(elem) 륹
목록에
추가
remove(int index)
: index 매개변수로
지정한
위치에
있는
객체륹
제거
remove(Object elem)
: 주어진
객체가
ArrayList 에
있으면
그
객체륹
제거
contains(Object elem)
: 객체
매개변수
elem 에
매치되는
것이
있으면
‘참읁
리턴
Array List (2/2) Array List (2/2)
목록에
isEmpty()
: 아무
원소도
없으면
‘참’읁
리턴
indexOf(Object elem)
: 객체
매개변수
(elem) 의
인덱스
또는
-1 읁
리턴
size()
목록에
: 현재
들어있는
원소의
개수륹
리턴
get(int index)
: 주어진
index 매개변수
위치에
있는
개체륹
리턴
Array List 로할수있는것(1/2) Array List 로할수있는것(1/2)
새로
만든다
ArrayList myList = new ArrayList();
ArrayList 라는
객체가
힙에
만들어짐
뭔가를집어넣는다Egg s = new Egg();
myList.add(s);
다른것을또집어넣는다Egg b = new Egg();
myList.add(s);
ArrayList 객체는Egg 객체를담기위한‘상자’가됨Egg 객체를담기위해ArrayList 객체가더커짐
Array List 로할수있는것(2/2) Array List 로할수있는것(2/2)
몆
개가
들어가
있는.
알아낸다
int theSize = myList.size();
boolean isIn = myList.contains(s);
int idx = myList.indexOf(b);
boolean empty = myList.isEmpty();
2개가
들어
있기
때문에
2륹
리턴
있기
때문에
true 리턴
때문에
‘1’읁
리턴
때문에
false 리턴
어떤것이안에들어있는지찾아냄‘s’라는Egg객체가들어
어떤것의위치(인덱스)를찾아냄‘b’는두번째로추가되었기
비어있는지확인두개가들어있기
목록에있는것을제거크기가줄어들어
myList.remove(s); ‘b’
Egg객체만
남음
일반배열과의차이점ArrayList
ArrayList myList = new
ArrayList();
String a = new String(“whoohoo”);
myList.add(a);
myList.add(b);
int theSize = myList.size();
myList.remove(1);
Boolean isIn = myList.contains(b);
일반배열String [] myList = new String[2];
String a = new String(“whoohoo”);
myList[0] = a;
myList[1] = b;
int theSize = myList.length;
myList[1] = null;
boolean isIn = false;
for (String item : myList) {
if (b.equals(item0) {
isIn = true;
break; }
}
일반배열과의차이점ArrayList
ArrayList myList = new
ArrayList();
String a = new String(“whoohoo”);
myList.add(a);
myList.add(b);
int theSize = myList.size();
myList.remove(1);
Boolean isIn = myList.contains(b);
일반배열String [] myList = new String[2];
String a = new String(“whoohoo”);
myList[0] = a;
myList[1] = b;
int theSize = myList.length;
myList[1] = null;
boolean isIn = false;
for (String item : myList) {
if (b.equals(item0) {
isIn = true;
break; }
}
DotCom 코드수정(1/2)
public class DotCom {
int [] locationCells;
int numOfHits = 0;
public void setLocationCells(int [] locs) {
locationCells = locs; }
public String checkYourself(String stringGuess) {
int guess = Integer.parseInt(stringGuess);
String result = "miss";
for(int i = 0; i < locationCells.length; i++) {
if (guess == locationCells[i]) {
result = "hit";
numOfHits++;
break; } }
if (numOfHits == locationCells.length) {
result = "kill"; }
System.out.println(result);
return result;
}
}
버그가있는기존버전DotCom 코드수정(1/2)
public class DotCom {
int [] locationCells;
int numOfHits = 0;
public void setLocationCells(int [] locs) {
locationCells = locs; }
public String checkYourself(String stringGuess) {
int guess = Integer.parseInt(stringGuess);
String result = "miss";
for(int i = 0; i < locationCells.length; i++) {
if (guess == locationCells[i]) {
result = "hit";
numOfHits++;
break; } }
if (numOfHits == locationCells.length) {
result = "kill"; }
System.out.println(result);
return result;
}
}
버그가있는기존버전
DotCom 코드수정(2/2)
import java.util.ArrayList;
public class DotCom {
private ArrayList locationCells;
public void setLocationCells (ArrayList loc) {
locationCells = loc;
}
public String checkYourself (String userInput) {
String result = "miss";
int index = locationCells.indexOf(userInput);
if (index>= 0) {
locationCells.remove(index);
if (locationCells.isEmpty()) {
result = "kill";
} else {
result = "hit";
}
}
return result;
}
}
ArrayList 채용Import 선언문배열대신ArrayList
사용자가추측한위치가ArrayList 에들어있는지확인index >= 0 이면추측한위치가목록에들어있는것이므로제거목록이비어있다면DotCom 코드수정(2/2)
import java.util.ArrayList;
public class DotCom {
private ArrayList locationCells;
public void setLocationCells (ArrayList loc) {
locationCells = loc;
}
public String checkYourself (String userInput) {
String result = "miss";
int index = locationCells.indexOf(userInput);
if (index>= 0) {
locationCells.remove(index);
if (locationCells.isEmpty()) {
result = "kill";
} else {
result = "hit";
}
}
return result;
}
}
ArrayList 채용Import 선언문배열대신ArrayList
사용자가추측한위치가ArrayList 에들어있는지확인index >= 0 이면추측한위치가목록에들어있는것이므로제거목록이비어있다면
부울 표현식
if (price >= 300 && price < 400) {
camera = “X”;
}
if (brand.equals(“A”) || brand.equals(“B”)) {
// A 브랜드 또는 B 브랜드에만
}
if ((zoomType.equals(“optical”) &&
(zoomDegree >= 3 && zoomDegree <= 8)) ||
(zoomType.equals(“digital”) &&
(zoomDegree >= 5 && zoomDegree <= 12))) {
// optical 이고 줌이 3이상 8 이하 또는
// digital 이고 줌이 5 이상 12 이하에만
}
AND 와 OR 연산자
if (model != 2000) {
// 2000 이라는 모델을
// 제외한 나머지
}
if ( ! brand.equals(“X”) ) {
// 브랜드가 X가 아닌
}
같지 않음
부울 표현식
if (price >= 300 && price < 400) {
camera = “X”;
}
if (brand.equals(“A”) || brand.equals(“B”)) {
// A 브랜드 또는 B 브랜드에만
}
if ((zoomType.equals(“optical”) &&
(zoomDegree >= 3 && zoomDegree <= 8)) ||
(zoomType.equals(“digital”) &&
(zoomDegree >= 5 && zoomDegree <= 12))) {
// optical 이고 줌이 3이상 8 이하 또는
// digital 이고 줌이 5 이상 12 이하에만
}
AND 와 OR 연산자
if (model != 2000) {
// 2000 이라는 모델을
// 제외한 나머지
}
if ( ! brand.equals(“X”) ) {
// 브랜드가 X가 아닌
}
같지 않음
API 패키지
System,
String,
Math …
java.lang
ArrayList
등 각종
유틸리티
java.util
스윙 GUI
클래스 중
일부
Javax.swing
API 패키지
System,
String,
Math …
java.lang
ArrayList
등 각종
유틸리티
java.util
스윙 GUI
클래스 중
일부
Javax.swing
클래스의 전체 이름
java . util . ArrayList
클래스의 전체 이름
java . util . ArrayList
패키지명 클래스명
자바에 어떤 ArrayList 를 사용할지 알려줘야 하는데 두 가지 방법 있음
소스 코드 파일 맨 위에서
import 선언문 사용
import java.util.ArryList;
public class MyClass { … }
import 선언문 사용
코드에서 언제 어디서나 직접
전체 이름을 입력
Java.util.ArrayList list =
new java.util.ArrayList();
일일이 입력
핵심정리(1/2)
ArrayList 는
자바
API에
포함되어있는
클래스이다
.
ArrayList 에서뭔가를제거할때는remove() 를사용한다.
ArrayList 에들어있는어떤것의위치를알고싶다면(또는들어ArrayList 에뭔가를집어넣을때는add()를사용한다.
있는지) indexOf() 륹
사용한다
.
ArrayList 가비어있는지확인할때는isEmpty() 를사용한다.
ArrayList 의크기를알고싶다면size()를사용한다.
일반배열의길이를알고싶다면length 변수를사용한다.
핵심정리(2/2) 핵심정리(2/2)
ArrayList 는
필요에
따라
그
크기가
동적으로
바뀐다
.
ArrayList 에저장할객체유형은유형이름을<> 안에집어넣은
형태의
유형
매개변수로
선언한다
.
ArrayList 에는원시유형의값은저장할수없고일반객체만
저장핝
수
있지만
, 컴파일러에서
원시
값읁
Object 객체로
감싸고
그
객체륹
원시유형이
아닌
ArrayList 에
저장하는
일읁
자동으로
처리해
준다
.
클래스는패키지단위로묶이고, java.lang 을제외한다른패키지의클래스를사용하려면전체이름을알려줘야한다.
소스코드맨위에서import 선언문을사용하거나
항상
전체
이름읁
입력해도
된다
.