Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- kotlin
- Android
- JPQL
- Thymeleaf
- Exception
- 자바
- Servlet
- 스프링 핵심 기능
- 알고리즘
- pointcut
- transaction
- 스프링 핵심 원리
- Proxy
- SpringBoot
- AOP
- 김영한
- java
- 인프런
- db
- 스프링
- 백준
- QueryDSL
- http
- springdatajpa
- jpa
- 그리디
- JDBC
- Spring Boot
- spring
- Greedy
Archives
- Today
- Total
개발자되기 프로젝트
List 인터페이스를 구현한 클래스, 활용 본문
1. ArrayList 활용
- Member 순차적 관리
2. Member
public class Member {
private int memberId;
private String memberName;
public Member(int memberId, String memberName) {
this.memberId = memberId;
this.memberName = memberName;
}
public int getMemberId() {
return memberId;
}
public void setMemberId(int memberId) {
this.memberId = memberId;
}
public String getMemberName() {
return memberName;
}
public void setMemberName(String memberName) {
this.memberName = memberName;
}
@Override
public String toString() {
return "Member{" +
"memberId=" + memberId +
", memberName='" + memberName + '\'' +
'}';
}
}
3. MemberArrayList
- Member 조회, 삭제, 추가 기능
- MemberId를 기준으로 조회, 삭제를 한다.
- 순서대로 돌아가면서 memberId가 일치하는지 확인한다.
- 하나씩 비교하기 때문에, data가 많아지면 최악의 경우 모든 data를 확인해야 한다.
public class MemberArrayList {
private ArrayList<Member> arrayList;
public MemberArrayList() {
arrayList = new ArrayList<>();
}
public MemberArrayList(int size) {
arrayList = new ArrayList<>(size);
}
public void addMember(Member member){
arrayList.add(member);
}
public Member getMember(int memberId){
for (int i = 0; i < arrayList.size(); i++){
Member member = arrayList.get(i);
int tempId = member.getMemberId();
if (tempId == memberId){
return arrayList.get(i);
}
}
return null;
}
public boolean removeMember(int memberId){
for (int i = 0; i < arrayList.size(); i++){
Member member = arrayList.get(i);
int tempId = member.getMemberId();
if (tempId == memberId){
arrayList.remove(i);
return true;
}
}
System.out.println("memberId :" + memberId + "존재하지 않습니다.");
return false;
}
public void showAllMember(){
for (Member member : arrayList) {
System.out.println("member = " + member);
}
System.out.println();
}
public int getSize(){
return arrayList.size();
}
}
4. Test
public class MemberArrayListTest {
MemberArrayList memberArrayList;
@BeforeEach
void beforeTest(){
memberArrayList = new MemberArrayList();
Member member1 = new Member(1001, "A");
Member member2 = new Member(1002, "B");
Member member3 = new Member(1003, "C");
Member member4 = new Member(1004, "D");
memberArrayList.addMember(member1);
memberArrayList.addMember(member2);
memberArrayList.addMember(member3);
memberArrayList.addMember(member4);
}
@Test
void addMemberTest(){
//given
Member member = new Member(10100, "F");
//when
memberArrayList.addMember(member);
Member findMember = memberArrayList.getMember(member.getMemberId());
//then
assertEquals(memberArrayList.getSize(), 5);
assertEquals(findMember.getMemberId(), member.getMemberId());
memberArrayList.showAllMember();
}
@Test
void removeMemberTest(){
//given
int removeMemberID = 1004;
//when
memberArrayList.removeMember(removeMemberID);
//then
assertEquals(memberArrayList.getSize(), 3);
assertNull(memberArrayList.getMember(removeMemberID));
memberArrayList.showAllMember();
}
}
5. GitHub: 211024 ArrayList
'Java > 자료구조' 카테고리의 다른 글
중복 허용하지 않는 Set 인터페이스 (0) | 2021.10.25 |
---|---|
Collection 요소를 순회하는 Iterator (0) | 2021.10.24 |
컬렉션 프레임워크 (0) | 2021.10.24 |
Generic Method (0) | 2021.10.24 |
<T extends Class> 사용 (0) | 2021.10.24 |
Comments