๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

[Java-๊ธฐ์ดˆ] List ์ธํ„ฐํŽ˜์ด์Šค

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 21.
๋ฐ˜์‘ํ˜•

โ–ถ List ์ธํ„ฐํŽ˜์ด์Šค

List ์ธํ„ฐํŽ˜์ด์Šค์—๋Š” ๊ฐ์ฒด๋ฅผ ์ˆœ์„œ์— ๋”ฐ๋ผ ์ €์žฅํ•˜๊ณ  ์œ ์ง€ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๋ฉ”์„œ๋“œ๊ฐ€ ์„ ์–ธ๋˜์–ด์žˆ๋‹ค.

ArrayList, Vector, LinkedList

โ€‹

โ€‹

 

*ArrayList ํด๋ž˜์Šค

๊ฐ์ฒด ์ˆœ์„œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ˆœ์ฐจ์ ์œผ๋กœ ์ž๋ฃŒ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌํ˜„ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

โ€‹

โ€‹MemberArrayList.java

package collection.arraylist;

import collection.Member;
import java.util.ArrayList;

public class MemberArrayList {
	private ArrayList<Member> arrayList;

	public MemberArrayList() {
		arrayList = new ArrayList<Member>();              
	}

	public void addMember(Member member) {  //  ArrayList์— ํšŒ์›์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฉ”์„œ๋“œ
		arrayList.add(member);
	}

	public boolean removeMember(int memberID) {  // ํ•ด๋‹น ์•„์ด๋””๋ฅผ ๊ฐ€์ง„ ํšŒ์›์„ ArrayList์—์„œ ์ฐพ์•„ ์ œ๊ฑฐํ•จ  
		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 + " ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");  // ๋ฐ˜๋ณต๋ฌธ์ด ๋๋‚ ๋•Œ๊นŒ์ง€ ํ•ด๋‹น ์•„์ด๋””๋ฅผ ์ฐพ์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ
		return false;
	}

	public void showAllMember() {  // ์ „์ฒด ํšŒ์›์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฉ”์„œ๋“œ
		for (Member member : arrayList) {
			System.out.println(member);
		}
		System.out.println();
	}
}

 

 

MemberArrayListTest.java

package collection.arraylist;

import collection.Member;

public class MemberArrayListTest {
	public static void main(String[] args) {
		MemberArrayList memberArrayList = new MemberArrayList();

		Member memberLee = new Member(1001, "์ด์ง€์›");
		Member memberSon = new Member(1002, "์†๋ฏผ๊ตญ");
		Member memberPark = new Member(1003, "๋ฐ•์„œํ›ค");
		Member memberHong = new Member(1004, "ํ™๊ธธ๋™");

		memberArrayList.addMember(memberLee);
		memberArrayList.addMember(memberSon);
		memberArrayList.addMember(memberPark);
		memberArrayList.addMember(memberHong);

		memberArrayList.showAllMember();

		memberArrayList.removeMember(memberHong.getMemberID());  // ํ™๊ธธ๋™ ํšŒ์› ์‚ญ์ œ
		memberArrayList.showAllMember();  // ํ™๊ธธ๋™ ํšŒ์›์„ ์‚ญ์ œํ•œ ํ›„ ๋‹ค์‹œ ์ „์ฒด ํšŒ์› ์ถœ๋ ฅ
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

 

 

๋‚˜ ํ˜ผ์ž ์ฝ”๋”ฉ) ArrayList์˜ ํŠน์ • ์œ„์น˜์— ํšŒ์› ์ถ”๊ฐ€ํ•˜๊ธฐ

ํšŒ์›์„ ์ถ”๊ฐ€ํ•  ๋•Œ ๋งจ ๋’ค๊ฐ€ ์•„๋‹Œ ํŠน์ • ์œ„์น˜์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค๊ณ , MemberArrayListTest ํด๋ž˜์Šค์— ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ํ…Œ์ŠคํŠธํ•ด๋ณด๋ผ.

ํžŒํŠธ. public void insertMember(Member member, int index) ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ MemberArrayList์— ๊ตฌํ˜„ํ•ด๋ณด๋ผ. ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋œ index ์œ„์น˜์— ํšŒ์›์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์ „์ฒด ํšŒ์›์„ ์ถœ๋ ฅํ•˜์—ฌ ํ™•์ธํ•ด ๋ณด์ž.

 

MemberArrayList.java

package collection.arraylist;

import collection.Member;
import java.util.ArrayList;
import java.util.Iterator;

public class MemberArrayList {
	private ArrayList<Member> arrayList;

	public MemberArrayList() {
		arrayList = new ArrayList<Member>();  // Member๋กœ ์„ ์–ธํ•œ ArrayList ์ƒ์„ฑ              
	}

	public void addMember(Member member) {
		arrayList.add(member);
	}

	public boolean removeMember(int memberID) {      // ๋ฉค๋ฒ„ ์•„์ด๋””๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์‚ญ์ œ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•จ
		for(int i = 0; i < arrayList.size(); i++) {  // ํ•ด๋‹น ์•„์ด๋””๋ฅผ ๊ฐ€์ง„ ๋ฉค๋ฒ„๋ฅผ ArrayList์—์„œ ์ฐพ์Œ                       
			Member member = arrayList.get(i);        // get() ๋ฉ”์„œ๋“œ๋กœ ํšŒ์›์„ ์ˆœ์ฐจ์ ์œผ๋กœ ๊ฐ€์ ธ์˜ด
			int tempId = member.getMemberID();
			if (tempId == memberID) {                // ํšŒ์› ์•„์ด๋””๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ์ผ์น˜ํ•˜๋ฉด
				arrayList.remove(i);                 // ํ•ด๋‹น ํšŒ์›์„ ์‚ญ์ œ
				return true;
			}
		}
		System.out.println(memberID + " ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");  // for๋ฌธ์ด ๋๋‚ ๋•Œ๊นŒ์ง€ ๋ฐ˜ํ™˜์ด ์•ˆ๋œ ๊ฒฝ์šฐ 
		return false;
	}

	public void showAllMember() {
		for (Member member : arrayList) {
			System.out.println(member);
		}
		System.out.println();
	}

	public void insertMember(Member member, int index) {
		arrayList.add(index, member);  // addMember() ๋ฉ”์„œ๋“œ
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

 

 

 

*ArrayList์™€ Vector ํด๋ž˜์Šค → ๋‘˜์ด ํฐ ์ฐจ์ด๋Š” ์—†์Œ

Vector๋Š” ์ž๋ฐ” 2 ์ด์ „๋ถ€ํ„ฐ ์ œ๊ณตํ–ˆ์œผ๋ฉฐ ArrayList์ฒ˜๋Ÿผ ๋ฐฐ์—ด์„ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค.

ArrayList์™€ Vector์˜ ๊ฐ€์žฅ ํฐ ์ฐจ์ด๋Š” ๋™๊ธฐํ™” ์ง€์› ์—ฌ๋ถ€์ด๋‹ค.

๋‘ ์ž‘์—…์ด ๋™์‹œ์— ์‹คํ–‰๋˜๋Š” ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์ด ์•„๋‹Œ ๊ฒฝ์šฐ์—๋Š” ArrayList๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ๊ถŒ์žฅํ•œ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ๋™๊ธฐํ™”๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋™์‹œ์— ์ž‘์—…์ด ์ด๋ฃจ์–ด์ง€๋Š” ์ž์›์— ๋Œ€ํ•ด ์ž ๊ธˆ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ฆ‰ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๋ฐฐ์—ด ๊ฐ์ฒด์— ์ž ๊ธˆ์„ ํ•˜๊ณ , ๋ฉ”์„œ๋“œ ์ˆ˜ํ–‰์ด ๋๋‚˜๋ฉด ์ž ๊ธˆ์„ ํ•ด์ œํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค. ์ด๋ ‡๊ฒŒ Vector์˜ ๋ชจ๋“  ๋ฉ”์„œ๋“œ๋Š” ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ์ž ๊ธˆ๊ณผ ํ•ด์ œ๊ฐ€ ์ผ์–ด๋‚˜๋ฏ€๋กœ ArrayList๋ณด๋‹ค ์ˆ˜ํ–‰ ์†๋„๊ฐ€ ๋Š๋ฆฌ๋‹ค. ArrayList๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ–ˆ๋Š”๋ฐ ๋‚˜์ค‘์— ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋™๊ธฐํ™”๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด Vector๋กœ ๋ฐ”๊พธ์ง€ ์•Š๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ArrayList ์ƒ์„ฑ ์ฝ”๋“œ๋ฅผ ์“ฐ๋ฉด ๋œ๋‹ค.

 

 

 

*์Šค๋ ˆ๋“œ์™€ ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ

์Šค๋ ˆ๋“œ๋ž€ ๊ฐ„๋‹จํžˆ ๋งํ•˜๋ฉด ์ž‘์—…๋‹จ์œ„์ด๋‹ค. ํ”„๋กœ๊ทธ๋žจ์ด ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์ˆ˜ํ–‰๋˜๋ ค๋ฉด ์Šค๋ ˆ๋“œ ์ž‘์—…์ด ์ƒ์„ฑ๋˜์–ด์•ผํ•œ๋‹ค. ์ด๋•Œ ํ•˜๋‚˜์˜ ์Šค๋ ˆ๋“œ๋งŒ ์ˆ˜ํ–‰๋˜๋ฉด ๋‹จ์ผ ์Šค๋ ˆ๋“œ๋ผ๊ณ  ํ•˜๊ณ  ๋‘ ๊ฐœ ์ด์ƒ์˜ ์Šค๋ ˆ๋“œ๊ฐ€ ๋™์‹œ์— ์‹คํ–‰๋˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ๋ผ๊ณ  ํ•œ๋‹ค. ๋‘ ๊ฐœ ์ด์ƒ์˜ ์Šค๋ ˆ๋“œ๊ฐ€ ๋™์‹œ์— ์‹คํ–‰๋˜๋ฉด ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์— ์ ‘๊ทผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€์ˆ˜ ๊ฐ’์ด๋‚˜ ๋ฉ”๋ชจ๋ฆฌ ์ƒํƒœ์— ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค. ์ด ๋•Œ ๋ฉ”๋ชจ๋ฆฌ์— ๋™์‹œ์— ์ ‘๊ทผํ•˜์ง€ ๋ชปํ•˜๋„๋ก ์ˆœ์„œ๋ฅผ ๋งž์ถ”๋Š” ๊ฒƒ์ด ๋™๊ธฐํ™”์ด๋‹ค.

โ€‹

MemberVector.java

package collection.arraylist;

import java.util.Vector;
import collection.Member;

public class MemberVector {
	private Vector<Member> vector;

	public MemberVector() {
		vector = new Vector<Member>();
	}

	public void addMember(Member member) {
		vector.add(member);
	}

	public boolean removeMember(int memberID) { 
		for (int i = 0; i < vector.size(); i++) { 
			Member member = vector.get(i); 
			int tempId = member.getMemberID();
			if (tempId == memberID) {
				vector.remove(i); 
				return true;
			}
		}
		System.out.println(memberID + " ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
		return false;
	}

	public void showAllMember() {
		for (Member member : vector) {
			System.out.println(member);
		}
		System.out.println();
	}

	public void insertMember(Member member, int index) {
		vector.add(index, member);
	}
}

 

 

MemberVectorTest.java

package collection.arraylist;

import collection.Member;

public class MemberVectorTest {
	public static void main(String[] args) {
		MemberVector memberVector = new MemberVector();

		Member memberLee = new Member(1001, "์ด์ง€์›");
		Member memberSon = new Member(1002, "์†๋ฏผ๊ตญ");
		Member memberPark = new Member(1003, "๋ฐ•์„œํ›ค");
		Member memberHong = new Member(1004, "ํ™๊ธธ๋™");

		memberVector.addMember(memberLee);
		memberVector.addMember(memberSon);
		memberVector.addMember(memberPark);
		memberVector.addMember(memberHong);

		memberVector.showAllMember();

		memberVector.removeMember(memberHong.getMemberID());
		memberVector.showAllMember();

		memberVector.insertMember(memberHong, 1);
		memberVector.showAllMember();
	}
}

 

 

 

*LinkedList ํด๋ž˜์Šค

ArrayList์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•œ ํด๋ž˜์Šค.

๊ฐ™์€ List ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ArrayList์— ๋น„ํ•ด ์ค‘๊ฐ„์— ์ž๋ฃŒ๋ฅผ ๋„ฃ๊ณ  ์ œ๊ฑฐํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์ด ์ ๊ฒŒ ๊ฑธ๋ฆฐ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๊ณ , ํฌ๊ธฐ๋ฅผ ๋™์ ์œผ๋กœ ์ฆ๊ฐ€์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

 

 

 

*ArrayList์™€ LinkedList์˜ ๋‹ค๋ฅธ ์ 

ArrayList๋Š” ์ƒ์„ฑํ•  ๋•Œ ์šฉ๋Ÿ‰์„ ์ง€์ •ํ•˜๊ณ  ์šฉ๋Ÿ‰๋ณด๋‹ค ๋” ๋งŽ์€ ์š”์†Œ๊ฐ€ ์ถ”๊ฐ€๋œ ๊ฒฝ์šฐ์— ์šฉ๋Ÿ‰์„ ๋Š˜๋ ค๊ฐ€๋ฉฐ ์ˆ˜ํ–‰ํ•œ๋‹ค.

LinkedList๋Š” ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ๋งˆ๋‹ค ๋™์ ์œผ๋กœ ์š”์†Œ์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์šฉ๋Ÿ‰์„ ๋Š˜๋ฆฌ๊ณ  ์š”์†Œ ๊ฐ’์„ ๋ณต์‚ฌํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์žˆ๋‹ค. ๋˜ํ•œ, ์ž๋ฃŒ๋ฅผ ์ค‘๊ฐ„์— ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•  ๋•Œ ์ž๋ฃŒ์˜ ์ด๋™์ด ArrayList๋ณด๋‹ค ์ ๋‹ค.

ํ•˜์ง€๋งŒ ArrayList๋Š” ์ž๋ฃŒ์˜ ์œ„์น˜๋ฅผ ์ฐพ์„ ๋•Œ ํŽธ๋ฆฌํ•˜๊ณ  LinkedList๋ณด๋‹ค ๊ตฌํ˜„ํ•˜๊ธฐ๋„ ์‰ฝ๋‹ค.

๋”ฐ๋ผ์„œ ์‚ฌ์šฉํ•˜๋Š” ์ž๋ฃŒ์˜ ๋ณ€๋™(์‚ฝ์ž…·์‚ญ์ œ)์ด ๋งŽ์€ ๊ฒฝ์šฐ์—๋Š” LinkedList๋ฅผ,

์ž๋ฃŒ ๋ณ€๋™์ด ๊ฑฐ์˜ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” ArrayList๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํšจ์œจ์ ์ด๋‹ค.

โ€‹

 

-LinkedList ํด๋ž˜์Šค ์‚ฌ์šฉํ•˜๊ธฐ

โ€‹

LinkedListTest.java

package collection;

import java.util.LinkedList;

public class LinkedListTest {
	public static void main(String[] args) {
		LinkedList<String> myList = new LinkedList<String>();

		myList.add("A");
		myList.add("B");
		myList.add("C");

		System.out.println(myList);  // ๋ฆฌ์ŠคํŠธ ์ „์ฒด ์ถœ๋ ฅ

		myList.add(1, "D");  // LinkedList์˜ ์ฒซ ๋ฒˆ์งธ ์œ„์น˜์— D ์ถ”๊ฐ€
		System.out.println(myList);

		myList.addFirst("0");  // ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ๋งจ ์•ž์— 0 ์ถ”๊ฐ€
		System.out.println(myList);

		System.out.println(myList.removeLast());  // ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ ๋งจ ๋’ค ์š”์†Œ ์‚ญ์ œ ํ›„ ํ•ด๋‹น ์š”์†Œ๋ฅผ ์ถœ๋ ฅ
		System.out.println(myList);
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€