Java

์ž๋ฐ”(Java)์˜ ArrayList ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ

567Rabbit 2024. 6. 3. 17:37

ArrayList ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ž€?

 

๊ธฐ์กด์˜ Array(๋ฐฐ์—ด)์€ ์ •ํ•ด์ง„ ๋ฐ์ดํ„ฐ์˜ ๊ฐฏ์ˆ˜ ์ด์ƒ์œผ๋กœ๋Š” ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€๊ฐ€ ๋ถˆ๊ฐ€ํ•˜๋‹ค

๊ฐฏ์ˆ˜ ์ œํ•œ ์—†์ด ๋ฐ์ดํ„ฐ๋ฅผ ๋งˆ์Œ๋Œ€๋กœ ์ถ”๊ฐ€, ์ˆ˜์ •, ์‚ญ์ œํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๋งŒ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ด๋‹ค.

 

 


ArrayList ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ๋ฒ•(๊ทœ์น™)

 

๋‹ด๊ณ ์‹ถ์€ ๋ฐ์ดํ„ฐ์˜ ํƒ€์ž…์„ < > ์•ˆ์— ์ ์–ด์ค˜์•ผ ํ•œ๋‹ค.


ArrayList<String> nameList = new ArrayList<>();

 

 

 

 

(1) ํŒจํ‚ค์ง€ ํ•˜๋‚˜์— ์ž‘์„ฑ

 

- ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ์ŠคํŠธ๋Ÿญ์ณ ๋งŒ๋“ค๊ธฐ

 

 

 

import java.util.ArrayList;   //ArrayList ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž„ํฌํŠธ

public class ArrayListMain {

	public static void main(String[] args) {
		
		// ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€
		nameList.add("ํ™๊ธธ๋™");
		nameList.add("๊น€๋‚˜๋‚˜");
		nameList.add("Mike");
		
		// ๋ฐ์ดํ„ฐ ์–ต์„ธ์Šค
		System.out.println(nameList.get(0));
		System.out.println(nameList.get(1));
		System.out.println(nameList.get(2));
		
		// ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค ์ถœ๋ ฅํ•˜์‹œ์˜ค.
		for(int i = 0 ; i < nameList.size() ; i++ ) { //nameList๋Š” length ๋Œ€์‹  size ์“ด๋‹ค
			System.out.println(nameList.get(i));
		}
		
		// for each ๋ฐ˜๋ณต๋ฌธ
		for( String name : nameList) {  //nameList์— ์žˆ๋Š” ๊ฒƒ์„ ํ•˜๋‚˜์”ฉ ๋นผ์„œ ์ €์žฅํ•œ๋‹ค
			System.out.println(name);
		}
		
		// ์ฒซ๋ฒˆ์งธ ๋ฐ์ดํ„ฐ ํ™๊ธธ๋™์„ ๊น€๊ธธ๋™์œผ๋กœ ๋ณ€๊ฒฝ (์ˆ˜์ •)
		nameList.set(0,"๊น€๊ธธ๋™");
		System.out.println(nameList.get(0));
		
        
		// ๋ฐ์ดํ„ฐ 1๊ฐœ ์‚ญ์ œํ•˜๊ธฐ
		// ์ธ๋ฑ์Šค๋กœ ์‚ญ์ œ
		nameList.remove(0);
		// ๊ฐ’์œผ๋กœ ์‚ญ์ œ
		nameList.remove("Mike");
        
        
		// ๋ชจ๋“  ๋ฐ์ดํ„ฐ ํ•œ๊บผ๋ฒˆ์— ์‚ญ์ œ
		nameList.clear();
		
		// ๋ฐ์ดํ„ฐ ๋น„์–ด์žˆ๋Š”์ง€ ์•„๋‹Œ์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜
		if( nameList.isEmpty() ) {
			System.out.println("๋น„์–ด์žˆ๋‹ค.");
		} else {
			System.out.println("๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค.");
		}
        
	}

}

 

 

 

 

 

(2) ํŒจํ‚ค์ง€ ์—ฌ๋Ÿฌ๊ฐœ์— ์ž‘์„ฑ

 

- ์ฃผ์†Œ๋ก์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ์ŠคํŠธ๋Ÿญ์ณ ๋งŒ๋“ค๊ธฐ

 

 

 

 

Person ํด๋ž˜์Šค ์ž‘์„ฑํ•˜๊ธฐ

package model;

public class Person {
	
	private String name;
	private String phone;
	
	public Person() { // ๋””ํดํŠธ ์ƒ์„ฑ์ž ์ƒ์„ฑ
		
	}
	
	// Constructor ์ƒ์„ฑ์ž ์ƒ์„ฑ
	public Person(String name, String phone) {
		this.name = name;
		this.phone = phone;
	}

	// getter and setter
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	

}

 

 

 

 

ArrayListMain ํด๋ž˜์Šค ์ž‘์„ฑํ•˜๊ธฐ

package main;

import java.util.ArrayList;

import model.Person;

public class ArrayListMain {

       public static void main(String[] args) {


              ArrayList<Person> personList = new ArrayList<>();


              Person p = new Person("ํ™๊ธธ๋™","010-2222-3333");

              personList.add(p);
              

              p = new Person("๊น€๋‚˜๋‚˜", "010-3333-4444");

              personList.add(p);
              

              p = new Person("Mike", "010-1111-1111");

              personList.add(p);
              

              // ์ฃผ์†Œ๋ก์˜ ํŠน์ • ์ธ๋ฌผ ์ถœ๋ ฅ
              Person person = personList.get(1);

              System.out.println(person.getName());
              System.out.println(person.getPhone());


              // ์ฃผ์†Œ๋ก์˜ ๋ชจ๋“  ์‚ฌ๋žŒ์˜ ์ด๋ฆ„๊ณผ ์ „ํ™”๋ฒˆํ˜ธ ์ถœ๋ ฅ
              for (Person p1 : personList) {
                 System.out.println(p1.getName() + " : " + p1.getPhone());
              }

              for (int i = 0; i < personList.size(); i++) {
                 Person p1 = personList.get(i);
                 System.out.println(p1.getName() + " : " + p1.getPhone());
              }

              // ๋‘๋ฒˆ์งธ ๋ฐ์ดํ„ฐ์ธ ๊น€๋‚˜๋‚˜์˜ ์ฃผ์†Œ๋ก ์‚ญ์ œ
              personList.remove(1);

              // ํ™๊ธธ๋™์˜ ์ „ํ™”๋ฒˆํ˜ธ 010-9999-8888๋กœ ๋ณ€๊ฒฝ
              Person p1 = personList.get(0);
              person.setPhone("010-9999-8888");

              // ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์‚ญ์ œ
              personList.clear();
       }
}

 

 

 

* person, p, p1 ์ฒ˜๋Ÿผ ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ๋‹ค์–‘ํ™” ํ•˜๋Š” ์ด์œ ๋Š”?

 

๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด์„œ: person, p, p1 ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์—ฌ ์„œ๋กœ ๋‹ค๋ฅธ ๋ฒ”์œ„(scope)์™€ ์šฉ๋„๋ฅผ ๊ตฌ๋ถ„ํ•˜๋ ค๋Š” ์˜๋„์ด๋‹ค. ํŠนํžˆ, ๋™์ผํ•œ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ณ€์ˆ˜์˜ ์˜๋ฏธ์™€ ์‚ฌ์šฉ ๋ชฉ์ ์ด ํ—ท๊ฐˆ๋ฆด ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ์ด๋ฆ„์„ ๋ถ€์—ฌํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค.

 

์ฝ”๋“œ ๋ธ”๋ก ๋‚ด์˜ ์ž„์‹œ ๋ณ€์ˆ˜: ๋ฐ˜๋ณต๋ฌธ์ด๋‚˜ ์ž„์‹œ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ณ€์ˆ˜๋Š” ์งง์€ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ๋ฐ˜๋ณต๋ฌธ ์•ˆ์—์„œ p1์ด๋ผ๋Š” ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ˜๋ณต๋ฌธ ๋‚ด๋ถ€์˜ ๋ณ€์ˆ˜์ž„์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.