[Java] ArrayList / HashSet ์ •๋ฆฌ

Jeiniยท2022๋…„ 11์›” 28์ผ
0

โ˜•๏ธย  Java

๋ชฉ๋ก ๋ณด๊ธฐ
39/59
post-thumbnail

๐Ÿ’กArrayList<T>


import java.util.*;

public class ArraryListAll {
    public static void main(String[] args) {
        ArrayList<Integer> arrList = new ArrayList<>();

        //add()๋ฉ”์„œ๋“œ: ์š”์†Œ์˜ ์ €์žฅ
        arrList.add(40);
        arrList.add(20);
        arrList.add(30);
        arrList.add(10);
        System.out.println(arrList.add(30));

        //for๋ฌธ๊ณผ get()๋ฉ”์„œ๋“œ: ์š”์†Œ์˜ ์ถœ๋ ฅ
        for(int i = 0; i < arrList.size(); i++) {
            System.out.println(arrList.get(i));
        }
        //remove() ๋ฉ”์„œ๋“œ: ์š”์†Œ์˜ ์ œ๊ฑฐ
        arrList.remove(1);

        //for-each๋ฌธ ๊ณผ get(): ์š”์†Œ์˜ ์ถœ๋ ฅ
        for(int e : arrList) {
            System.out.println(e + " ");
        }
        //Collections.sort() ๋ฉ”์„œ๋“œ: ์š”์†Œ์˜ ์ •๋ ฌ
        Collections.sort(arrList);

        //iterator()๊ณผ get(): ์š”์†Œ์˜ ์ถœ๋ ฅ
        Iterator<Integer> iter = arrList.iterator();
        while(iter.hasNext()) {
            System.out.println(iter.next() + " ");
        }
        //set(): ์š”์†Œ์˜ ๋ณ€๊ฒฝ
        arrList.set(0, 20);

        //size(): ์š”์†Œ์˜ ์ด ๊ฐœ์ˆ˜
        System.out.println("๋ฆฌ์ŠคํŠธ์˜ ํฌ๊ธฐ: " + arrList.size());


    }
}

๐Ÿ’กHashSet<T>


import java.util.*;

public class SetAll {
    public static void main(String[] args) {
        HashSet<String> hs1 = new HashSet<>();
        HashSet<String> hs2 = new HashSet<>();

        //hs1-add(): ์š”์†Œ์˜ ์ €์žฅ
        hs1.add("ํ™๊ธธ๋™");
        hs1.add("์ด์ˆœ์‹ ");
        System.out.println(hs1.add("์ž„๊บฝ์ •"));
        System.out.println(hs1.add("์ž„๊บฝ์ •")); // ์ค‘๋ณต๋œ ์š”์†Œ์˜ ์ €์žฅ

        //for-each: ์š”์†Œ์˜ ์ถœ๋ ฅ
        for(String e : hs1) {
            System.out.println(e + " ");
        }
        //hs2-add(): ์š”์†Œ์˜ ์ €์žฅ
        hs2.add("์ž„๊บฝ์ •");
        hs2.add("ํ™๊ธธ๋™");
        hs2.add("์ด์ˆœ์‹ ");

        //iterator(): ์š”์†Œ์˜ ์ถœ๋ ฅ
        Iterator<String> ite = hs2.iterator();
        while(ite.hasNext()) {
            System.out.println(ite.next() + " ");
        }
        //size(): ์š”์†Œ์˜ ์ด ๊ฐœ์ˆ˜
        System.out.println("์ง‘ํ•ฉ์˜ ํฌ๊ธฐ: " + hs2.size());


    }
}
profile
Fill in my own colorful colors๐ŸŽจ

0๊ฐœ์˜ ๋Œ“๊ธ€