Notice
Recent Posts
Recent Comments
Link
«   2025/01   »
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
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

List 인터페이스를 구현한 클래스, 활용 본문

Java/자료구조

List 인터페이스를 구현한 클래스, 활용

Seung__ 2021. 10. 24. 23:31

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


 

GitHub - bsh6463/dataStructure

Contribute to bsh6463/dataStructure development by creating an account on GitHub.

github.com

 

'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