[SelectAll/getAll] - 전체 탐색 > 반환값이 2개 이상
1) 따끈따끈한 배열 생성하기 (=따따배)
public ArrayList<PersonDTO> selectAll() { //R 전부 반환해야하니까 배열을 반환
ArrayList<PersonDTO> datas = new ArrayList<>(); //카피데이터 객체 생성
2) 그 배열에 복사데이터 넣기
for(int i=0; i<this.datas.size();i++) { //원본 배열 숫자만큼 복사(this 중요)
int num = this.datas.get(i).getNum(); //원본의 넘버 복사
String name = this.datas.get(i).getName(); //원본의 이름 복사
String type = this.datas.get(i).getType(); //원본의 타입 복사
datas.add(new PersonDTO(num,name,type));//복사한 값으로 카피객체 추가
3) 복사데이터 내보내기
return datas; //this.가 붙으면 원본데이터
▼합쳐서 코딩하면 아래와 같이 완성!
public ArrayList<PersonDTO> selectAll() { //R 전부 반환해야하니까 배열을 반환
ArrayList<PersonDTO> datas = new ArrayList<>(); //카피데이터 객체 생성
for(int i=0; i<this.datas.size();i++) { //원본 배열 숫자만큼 복사(this 중요)
int num = this.datas.get(i).getNum(); //원본의 넘버 복사
String name = this.datas.get(i).getName(); //원본의 이름 복사
String type = this.datas.get(i).getType(); //원본의 타입 복사
datas.add(new PersonDTO(num,name,type));//복사한 값으로 카피객체 추가
}
return datas; //this.가 붙으면 원본데이터
}
========================================================
[SelectOne/getOne] - 1개 탐색 > 반환값이 무조건 1개
1) PK값을 기준으로 정보 탐색 - 즉 인자는 PK값
public PersonDTO selectOne(int num) { //R 한명만 반환이니까 DTO 고정으로 반환
for(int i=0; i<this.datas.size(); i++) {//저장된 데이터만큼 찾으면서
2) 찾은 정보로 복사데이터 생성
if(this.datas.get(i).getNum()==num) {//입력받은 PK값과 동일한 것 찾기
String name = this.datas.get(i).getName(); //찾은 원본의 이름 복사
String type = this.datas.get(i).getType(); //찾은 원본의 타입 복사
PersonDTO data = new PersonDTO(num,name,type);//찾은 카피 데이터로 객체생성
3) 복사데이터로 만든 클론 객체 전달
return data; //클론데이터 전달
}
}
return null; //없으면 null반환
▼합쳐서 코딩하면 아래와 같이 완성!
public PersonDTO selectOne(int num) { //R 한명만 반환이니까 DTO 고정으로 반환
for(int i=0; i<this.datas.size(); i++) {//저장된 데이터만큼 찾으면서
if(this.datas.get(i).getNum()==num) {//입력받은 PK값과 동일한 것 찾기
String name = this.datas.get(i).getName(); //찾은 원본의 이름 복사
String type = this.datas.get(i).getType(); //찾은 원본의 타입 복사
PersonDTO data = new PersonDTO(num,name,type);//찾은 카피 데이터로 객체생성
return data; //클론데이터 전달
}
}
return null; //없으면 null반환
}
'개주 훈련일지 > 🏋️ 전집중 호흡 훈련' 카테고리의 다른 글
| 줄바꿈을 입력하는 방법 (0) | 2025.10.24 |
|---|---|
| 코드 응집도는 높이고 결합도는 낮춰라! (0) | 2025.10.24 |
| 정수만 입력 받기 (나머진 모두 예외처리) (0) | 2025.10.24 |
| 이터레이터 복사체 활용법 (0) | 2025.10.23 |
| 객체를 보여줄 수 있는 함수 toString (0) | 2025.10.22 |