๐Ÿ‘ฉโ€๐ŸŽ“๊ณต๋ถ€-์ž๋ฐ” ArrayList

28220 ๋‹จ์–ด ์ž๋ฐ”ArrayListArrayList


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


ArrayList

๊ธฐ์กด์— ๋ฐฐ์—ด์€ ๋ฌธ์ œ์ ์ด ๋‘๊ฐ€์ง€๊ฐ€ ์žˆ๋Š”๋ฐ

1 . ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด ์ˆ˜๊ฐ€ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•  ๋•Œ ๊ฒฐ์ •
2 . ๊ฐ์ฒด๋ฅผ ์‚ญ์ œํ–ˆ์„ ๋•Œ ํ•ด๋‹น ์ธ๋ฑ์Šค๊ฐ€ ๋น„๊ฒŒ ๋จ

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

1 . ๊ฐ์ฒด๋ฅผ ์ธ๋ฑ์Šค๋กœ ๊ด€๋ฆฌ
2 . ๊ฐ์ฒด ์ €์žฅ ์‹œ ์ž๋™ ์ธ๋ฑ์Šค๊ฐ€ ๋ถ€์—ฌ
3 . ์ค‘๋ณตํ•ด์„œ ๊ฐ์ฒด ์ €์žฅ์ด ๊ฐ€๋Šฅ
4 . ๊ฐ์ฒด ์ž์ฒด๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ๊ฐ์ฒด ๋ฒˆ์ง€๋ฅผ ์ฐธ์กฐ
5 . ์ถ”๊ฐ€, ์‚ญ์ œ, ๊ฒ€์ƒ‰ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ๋ฉ”์†Œ๋“œ ์ œ๊ณต

์˜ค๋Š˜ ๋‹ค๋ค„๋ณผ ๊ฒƒ์€ ๊ทธ์ค‘ ๊ตฌํ˜„ ํด๋ž˜์Šค์˜ ํ•˜๋‚˜์ธ ArrayList์ž…๋‹ˆ๋‹ค.

class Student {
	private String name;
	private String phone;
	public Student() {

this(โ€œNoneโ€, โ€œNoneโ€);
 }
 
public Student(String name, String phone) {
	this.name = name;
	this.phone = phone;
}
public String getName() {
	return name;
}
public String getPhone() {
	return phone;
}

@Override
public String toString() {
	return "Student [name=" + name + ", phone=" + phone + "]";
	}
}

๋ฌธ์ œ

์ด๋Ÿฐ ์ฝ”๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ArrayList ๊ฐ์ฒด์— ๋ฐฐ์—ด์„ ๋‹ด๊ณ 
์ด๋Ÿฐ์”ฉ์œผ๋กœ ์ถœ๋ ฅ์„ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด ํ•ด์•ผํ•  ๊ฒƒ์„ ์ •๋ฆฌํ•ด๋ด…์‹œ๋‹ค.

1 . alist ๋งŒ๋“ค๊ธฐ
2 . alist์— ํ•™์ƒ ์ •๋ณด ์ €์žฅ
3 . alist์˜ ๋ชจ๋“  ์›์†Œ ์ถœ๋ ฅ
4 . ์ด๋ฆ„ scanner๋กœ ์ž…๋ ฅ๋ฐ›๊ณ  ์ „๋ฒˆ์ถœ๋ ฅ
5 . ์ด๋ฆ„ ์ž…๋ ฅ๋ฐ›๊ณ  ํ•ด๋‹น ์›์†Œ ์‹น๋‹ค์ง€์šฐ๊ธฐ

๋ฌธ์ œ ์ž์ฒด๊ฐ€ ArrayList๋ฅผ ์ตํžˆ๊ธฐ์— ๋”ฑ ์‹ฌํ”Œํ•˜๊ณ  ์ข‹๋„ค์š”. ์ฒ˜์Œ ์จ๋ดค๋Š”๋ฐ 20๋ถ„์ด๋ฉด ๋˜๋”๋ผ๊ณ ์š”.
1๋ฒˆ๋ถ€ํ„ฐ ์ฐจ๊ทผ์ฐจ๊ทผ ํ’€์–ด๋ด…์‹œ๋‹ค.

alist ๋งŒ๋“ค๊ธฐ

List<Student> alist = new ArrayList<Student>();

๊ฐ„๋‹จํ•˜์ฃ ? ์ œ๋„ค๋ฆญ์ปฌ๋ ‰์…˜์ด๋ผ๋Š” ๊ฒƒ ์ž์ฒด๊ฐ€ ํƒ€์ž…์„ ํŒŒ๋ผ๋งคํ„ฐ๋กœ ๊ฐ€์ง€๊ธฐ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์„œ๋Š” Student๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ํƒ€์ž…์œผ๋กœ ๊ฐ€์ง€๋ฉด ๋˜๊ฒ ์ฃ ?

alist์— ํ•™์ƒ ์ •๋ณด ์ €์žฅ

		alist.add(new Student("name0","phone0"));
		alist.add(new Student("name1","phone1"));
		alist.add(new Student("name2","phone2"));
		alist.add(new Student("name3","phone3"));
		alist.add(new Student("name4","phone4"));

๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€์‹œ์ผœ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ฒ˜์Œ์— ๊ฐ์ฒด๋ฅผ ์–ด๋–ป๊ฒŒ ์ถ”๊ฐ€์‹œํ‚ฌ์ง€ ๋ชฐ๋ผ์„œ ํ•ด๋งธ๋Š”๋ฐ ํŒŒ๋ผ๋ฉ”ํ„ฐ๊ฐ’์ด ์–ด์งœํ”ผ Student์—ฌ์„œ ๊ทธ๋ƒฅ new๋กœ ์ƒ์„ฑ์ž์— ๋„ฃ์–ด์ฃผ๋ฉด ๋˜๋”๋ผ๊ตฌ์š”.

alist์˜ ๋ชจ๋“  ์›์†Œ ์ถœ๋ ฅ

for(Student std : alist)
{
	System.out.println(std);
}

๋ถˆ๊ณผ ์„œ๋„ˆ๋‹ฌ ์ „๋งŒ ํ•ด๋„ ๊ฐ์ฒด ๋ฐ˜๋ณต๋ฌธ์ด ๋ถˆํŽธํ–ˆ์—ˆ๋Š”๋ฐ ์ข€ ๊ณต๋ถ€ํ•˜๋‹ค๋ณด๋‹ˆ ํŽธํ•ด์กŒ์Šต๋‹ˆ๋‹ค.
std๋ž€ ๋ณ€์ˆ˜๋ฅผ ์ด์šฉ. alist์—์„œ ์ฐจ๋ก€๋Œ€๋กœ ๊ฐ์ฒด๋ฅผ ๊บผ๋‚ด์„œ student์—๋‹ค ๋„ฃ๊ฒ ๋‹ค๋Š” ์†Œ๋ฆฌ์ž…๋‹ˆ๋‹ค.

์ด๋ฆ„ scanner๋กœ ์ž…๋ ฅ๋ฐ›๊ณ  ์ „๋ฒˆ์ถœ๋ ฅ

Scanner input = new Scanner(System.in);
		System.out.print("์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์‹œ๋ฉด ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ๋“œ๋ฆฝ๋‹ˆ๋‹ค : ");
		String name = input.next();
		
		for(int i=0; i<alist.size(); i++)
		{
			if(name.equals(alist.get(i).getName()))
			System.out.print("name"+Integer.toString(i)+"์˜ ์ „ํ™”๋ฒˆํ˜ธ = "+alist.get(i).getPhone());
		}
		System.out.println("");System.out.println("");
		

์Šค์บ๋„ˆ๋กœ ์ž…๋ ฅ๋ฐ›๊ณ  for๋ฌธ ๋Œ๋ ค์„œ name์ค‘์—์„œ ๊ฐ™์€๊ฒŒ ์žˆ์œผ๋ฉด getPhoneํ•ด์„œ ํฐ๋ฒˆ ์ถœ๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

System.out.print("์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์‹œ๋ฉด ์ •๋ณด๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค : ");
		String del = input.next();
		for(int j=0; j<alist.size(); j++)
		{
			if(del.equals(alist.get(j).getName()))
					{
						alist.remove(j);
					}
		}
		for(Student std : alist)
		{
			System.out.println(std);
		}

์œ„์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•˜๋Š”๋ฐ remove๋กœ ๋ฐฐ์—ด ํ•˜๋‚˜ ํ†ต์ฑ„๋กœ ์ง€์›Œ์ฃผ๋ฉด ๋!


์‹ฌํ”Œํ•œ ๋ฌธ์ œ์˜€์ง€๋งŒ ArrayList ๊ธฐ์ดˆ๋ฅผ ์ตํžˆ๊ธฐ์— ๋ถ€์กฑํ•จ์ด ์—†๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.
๋‹ค์Œ ์ฑ•ํ„ฐ๊ฐ€ ํŒŒ์ผ ์ž…์ถœ๋ ฅ์ด๋˜๊ฐ€? ์˜€์œผ๋‹ˆ ๋ฏธ๋ฆฌ ์ข€ ์‚ดํŽด๋ด์•ผ๊ฒ ๋„ค์š”.
์•„๋ž˜ ์ฝ”๋“œ ์ „๋ฌธ ์ฒจ๋ถ€ํ•ฉ๋‹ˆ๋‹ค.


import java.util.*;
public class Test {
	
	public static void main(String args[])
	{
		System.out.println("20215242 ์œ ๋ น๊ฐœ ArrayList ์—ฐ์Šต๋ฌธ์ œ ํ’€์ด");
		//ํ•™์ƒ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” alist ์ƒ์„ฑ
		List<Student> alist = new ArrayList<Student>();
		
		//์‹คํ–‰ํ™”๋ฉด๊ณผ ๊ฐ™์ด alist์— ํ•™์ƒ ์ •๋ณด๋ฅผ ์ €์žฅ
		alist.add(new Student("name0","phone0"));
		alist.add(new Student("name1","phone1"));
		alist.add(new Student("name2","phone2"));
		alist.add(new Student("name3","phone3"));
		alist.add(new Student("name4","phone4"));
		
		//alist์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ถœ๋ ฅ
		for(Student std : alist)
		{
			System.out.println(std);
		}
		System.out.println("");
		
		//์‚ฌ์šฉ์ž์—๊ฒŒ ์ด๋ฆ„์„ ์ž…๋ ฅ ๋ฐ›์•„ ์ „ํ™”๋ฒˆํ˜ธ ๊ฒ€์ƒ‰ ํ›„ ํ•ด๋‹น ๋‚ด์šฉ ์ถœ๋ ฅ
		Scanner input = new Scanner(System.in);
		System.out.print("์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์‹œ๋ฉด ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ๋“œ๋ฆฝ๋‹ˆ๋‹ค : ");
		String name = input.next();
		
		for(int i=0; i<alist.size(); i++)
		{
			if(name.equals(alist.get(i).getName()))
			System.out.print("name"+Integer.toString(i)+"์˜ ์ „ํ™”๋ฒˆํ˜ธ = "+alist.get(i).getPhone());
		}
		System.out.println("");System.out.println("");
		
		//์‚ฌ์šฉ์ž์—๊ฒŒ ์ด๋ฆ„์„ ์ž…๋ ฅ ๋ฐ›์•„ ๋ฐ์ดํ„ฐ ์‚ญ์ œ ํ›„ alist์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ถœ๋ ฅ
		System.out.print("์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์‹œ๋ฉด ์ •๋ณด๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค : ");
		String del = input.next();
		for(int j=0; j<alist.size(); j++)
		{
			if(del.equals(alist.get(j).getName()))
					{
						alist.remove(j);
					}
		}
		for(Student std : alist)
		{
			System.out.println(std);
		}
		
	}
}

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ