๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[JAVA ์ฝ”๋”ฉ ์ดˆ๋ณด] List (๋ฆฌ์ŠคํŠธ) Method ๋ณธ๋ฌธ

๐Ÿฆฅ ์ฝ”ํ…Œ/JAVA

[JAVA ์ฝ”๋”ฉ ์ดˆ๋ณด] List (๋ฆฌ์ŠคํŠธ) Method

์ง•์ง•์•ŒํŒŒ์นด 2023. 7. 16. 16:27
728x90
๋ฐ˜์‘ํ˜•

<๋ณธ ๋ธ”๋กœ๊ทธ๋Š” codechacha ๋‹˜์˜ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค :-)>

 

๐Ÿ’— List Method

 

๐Ÿ‰ ์š”์†Œ ์‚ฝ์ž… & ํŠน์ • ์ธ๋ฑ์Šค ์‚ฝ์ž… & ๋ณ‘ํ•ฉ & ๋ฐ˜ํ™˜ & ์‚ญ์ œ

import java.util.*;
import java.io.*;

// tip: each public class is put in its own file
public class main
{
    // tip: arguments are passed via the field below this editor
    public static void main(String[] args)
    {
        List<String> list = new ArrayList<>();
        List<String> list2 = new ArrayList<>();

        //์š”์†Œ ์‚ฝ์ž…
        list.add("one");

        //ํŠน์ • ์ธ๋ฑ์Šค์— ์š”์†Œ ์‚ฝ์ž…
        list.add(0, "zero");
        System.out.println(list);

        //๋ฆฌ์ŠคํŠธ ๋ณ‘ํ•ฉ (์ถ”๊ฐ€๋˜๋Š” ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋’ค๋กœ ์˜จ๋‹ค)
        list.addAll(list2);

        //ํŠน์ • ์š”์†Œ์˜ ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
        list.indexOf("zero");        // 0

        //ํŠน์ • ์š”์†Œ์˜ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
        System.out.println(list.lastIndexOf("zero"));

        //ํŠน์ • ์ธ๋ฑ์Šค์˜ ๊ฐ’ ์‚ญ์ œ
        list.remove(0);

        //์ฒซ๋ฒˆ์งธ ๊ฐ’ ์‚ญ์ œ
        list.remove("one");
        System.out.println(list);
    }
}

 

๐Ÿ‰ ๋ฆฌ์ŠคํŠธ ์ฐจ์ง‘ํ•ฉ & ๊ต์ง‘ํ•ฉ & ๋น„์šฐ๊ธฐ & null ์ฒดํฌ & ๊ธธ์ด

import java.util.*;
import java.io.*;

// tip: each public class is put in its own file
public class main {
    // tip: arguments are passed via the field below this editor
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        List<String> list2 = new ArrayList<>();

        // ๋ฆฌ์ŠคํŠธ ์ฐจ์ง‘ํ•ฉ
        list.removeAll(list2); // list์—์„œ list2์— ์žˆ๋Š” ๋ชจ๋“  ๊ฐ’์„ ์‚ญ์ œ

        // ๋ฆฌ์ŠคํŠธ ๊ต์ง‘ํ•ฉ
        list.retainAll(list2); // list์—์„œ list2์— ์žˆ๋Š” ๊ฐ’์„ ์ œ์™ธํ•œ ๋ชจ๋“  ๊ฐ’์„ ์‚ญ์ œ

        // ๋ฆฌ์ŠคํŠธ ๋น„์šฐ๊ธฐ
        list.clear();

        // ๋ฆฌ์ŠคํŠธ ๋น„์–ด์žˆ๋Š”์ง€ ์ฒดํฌ
        System.out.println(list.isEmpty());

        // ๋ฆฌ์ŠคํŠธ ๊ธธ์ด
        System.out.println(list.size());
    }
}

 

๐Ÿ‰ Array ์™€ List ๋ณ€ํ™˜

import java.util.*;
import java.io.*;

// tip: each public class is put in its own file
public class main
{
    // tip: arguments are passed via the field below this editor
    public static void main(String[] args)
    {
        // asList : ๋ฌธ์ž์—ด ํƒ€์ž… ๋ฐฐ์—ด์„ List๋กœ ๋ณ€ํ™˜
        String[] temp = {"apple", "banana", "lemon"};
        List<String> list = new ArrayList<>(Arrays.asList(temp));
        System.out.println(list);

    }
}

import java.util.*;
import java.io.*;

// tip: each public class is put in its own file
public class main
{
    // tip: arguments are passed via the field below this editor
    public static void main(String[] args)
    {
        // toArray : List๋ฅผ ๋ฌธ์ž์—ด ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜
        List<String> list = new ArrayList<>();
        String[] temp = list.toArray(new String[list.size()]);
        System.out.println(temp);
    }
}

import java.util.*;
import java.io.*;

// tip: each public class is put in its own file
public class main
{
    // tip: arguments are passed via the field below this editor
    public static void main(String[] args)
    {
        // asList : ์ •์ˆ˜ ๋ฐฐ์—ด์„ List๋กœ ๋ณ€ํ™˜
        int[] temp = {1, 2, 3, 4};
        List<Integer> list = new ArrayList<>(Arrays.asList(temp));
        System.out.println(list);

        // toArray : List๋ฅผ ์ •์ˆ˜ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜
        List<Integer> list = new ArrayList<>();
        int[] temp = list.stream().mapToInt(x -> x).toArray();
    }
}

 

728x90
๋ฐ˜์‘ํ˜•
Comments