๊ด€๋ฆฌ ๋ฉ”๋‰ด

ruriruriya

[JAVA] ์ž๋ฐ” ๋ฆฌ์ŠคํŠธ(List)์˜ ArrayList ๋ณธ๋ฌธ

๐ŸชJava

[JAVA] ์ž๋ฐ” ๋ฆฌ์ŠคํŠธ(List)์˜ ArrayList

๋ฃจ๋ฆฌ์•ผใ…‘ 2023. 11. 13. 17:11
๋ฐ˜์‘ํ˜•

์‚ฌ์ง„: Unsplash ์˜ Maksym Zakharyak

์ž๋ฐ” ๋ฆฌ์ŠคํŠธ(List)๋Š” ๋ฐฐ์—ด๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ ๋ฐฐ์—ด๋ณด๋‹ค ๋” ๋งŽ์ด ์“ฐ์ด๋Š” ์ž๋ฃŒํ˜•์ด๋‹ค.
๋ฐฐ์—ด์€ ํฌ๊ธฐ๊ฐ€ ์ •ํ•ด์ ธ ์žˆ์ง€๋งŒ ๋ฆฌ์ŠคํŠธ๋Š” ํฌ๊ธฐ๊ฐ€ ๊ณ„์† ๋ณ€ํ•œ๋‹ค. 

๊ทธ๋ž˜์„œ ๋ฆฌ์ŠคํŠธ๋Š” ํฌ๊ธฐ๊ฐ€ ์ •ํ•ด์ ธ ์žˆ์ง€ ์•Š์•„์„œ ์›ํ•˜๋Š” ๋งŒํผ ๊ฐ’์„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.
ํฌ๊ธฐ๊ฐ€ ์ •ํ•ด์ ธ ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ๋ฉด ๋ฐฐ์—ด๋ณด๋‹ค ๋ฆฌ์ŠคํŠธ ์‚ฌ์šฉ์„ ์ถ”์ฒœํ•œ๋‹ค.

 

ArrayList

๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ArrayList์—๋Š” add, get, size, contains, remove ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ๋‹ค.

add ๋ฉ”์†Œ๋“œ

ArrayList์—์„œ add ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์š”์†Œ๊ฐ’์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.
์ˆ˜ํ•™์ ์ˆ˜๋ฅผ ์˜ˆ์‹œ๋กœ ๋“ค์–ด 1ํ•™๊ธฐ ์ค‘๊ฐ„๊ณ ์‚ฌ, 1ํ•™๊ธฐ ๊ธฐ๋ง๊ณ ์‚ฌ, 2ํ•™๊ธฐ ์ค‘๊ฐ„๊ณ ์‚ฌ ์ ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ–ˆ๋‹ค.

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList mathScore = new ArrayList();
        mathScore.add("90");
        mathScore.add("80");
        mathScore.add("85");
    }
}

๊ทผ๋ฐ 2ํ•™๊ธฐ ๊ธฐ๋ง๊ณ ์‚ฌ ์„ฑ์ ์„ ๋งจ ๋’ค์— ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์ธ๋ฑ์Šค ๊ฐ’์„ ์•Œ๋ ค์ฃผ์–ด ํ•ด๋‹น ์œ„์น˜์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค!
*์ธ๋ฑ์Šค๋Š” ํ•ญ์ƒ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค.

mathScore.add(3,"70"); // 4๋ฒˆ์งธ ์œ„์น˜์— 70 ์‚ฝ์ž…

 

get ๋ฉ”์†Œ๋“œ

ArrayList์—์„œ get ๋ฉ”์†Œ๋“œ๋Š” ํŠน์ • ์ธ๋ฑ์Šค ๊ฐ’์„ ์ถ”์ถœํ•œ๋‹ค.
1ํ•™๊ธฐ ๊ธฐ๋ง๊ณ ์‚ฌ ์ ์ˆ˜๋ฅผ ์ถ”์ถœํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์•„๋ž˜์˜ค ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค.

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList mathScore = new ArrayList();
        mathScore.add("90");
        mathScore.add("80");
        mathScore.add("85");
        mathScore.add(3,"70");
        System.out.println(mathScore.get(1)); // 80 ์ถœ๋ ฅ
    }
}

 

size ๋ฉ”์†Œ๋“œ

size ๋ฉ”์†Œ๋“œ๋Š” ArrayList์˜ ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
mathScore์— ๋‹ด๊ธด ์š”์†Œ๊ฐ€ ๋ช‡๊ฐœ์ธ์ง€ ์•Œ์•„๋ณด๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค.

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList mathScore = new ArrayList();
        mathScore.add("90");
        mathScore.add("80");
        mathScore.add("85");
        mathScore.add(3,"70");
        System.out.println(mathScore.size()); // 4 ์ถœ๋ ฅ
    }
}

 

contains ๋ฉ”์†Œ๋“œ

contains ๋ฉ”์†Œ๋“œ๋Š” ๋ฆฌ์ŠคํŠธ ์•ˆ์— ํ•ด๋‹น ํ•ญ๋ชฉ์ด ์žˆ๋Š” ์ง€ boolean ๊ฐ’์ธ true/false ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList mathScore = new ArrayList();
        mathScore.add("90");
        mathScore.add("80");
        mathScore.add("85");
        mathScore.add(3,"70");
        System.out.println(mathScore.contains("80")); // true ์ถœ๋ ฅ
        System.out.println(mathScore.contains("60")); // false ์ถœ๋ ฅ
    }
}

 

remove ๋ฉ”์†Œ๋“œ

remove ๋ฉ”์†Œ๋“œ๋Š” remove(๊ฐ์ฒด), remove(์ธ๋ฑ์Šค) ์ด๋ ‡๊ฒŒ ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์ด ์“ฐ์ธ๋‹ค.
์ฒซ ๋ฒˆ์งธ remove(๊ฐ์ฒด)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ์ฒด์— ํ•ด๋‹นํ•˜๋Š” ํ•ญ๋ชฉ์„ ์‚ญ์ œํ•˜๊ณ  boolean ๊ฐ’์œผ๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList mathScore = new ArrayList();
        mathScore.add("90");
        mathScore.add("80");
        mathScore.add("85");
        mathScore.add(3,"70");
        System.out.println(mathScore.remove("80")); // true ์ถœ๋ ฅ
    }
}

๋‘ ๋ฒˆ์งธ remove(์ธ๋ฑ์Šค) ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น ์ธ๋ฑ์Šค์— ํ•ด๋‹นํ•˜๋Š” ํ•ญ๋ชฉ์„ ์‚ญ์ œํ•œ ๋’ค ๊ทธ ํ•ญ๋ชฉ์„ ๋ฆฌํ„ดํ•œ๋‹ค.

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList mathScore = new ArrayList();
        mathScore.add("90");
        mathScore.add("80");
        mathScore.add("85");
        mathScore.add(3,"70");
        System.out.println(mathScore.remove(0)); // 90 ์ถœ๋ ฅ
    }
}
๋ฐ˜์‘ํ˜•