728x90
๋ฐ˜์‘ํ˜•

6) ๋ฐฐ์—ด

 

๋ฐฐ์ ์˜ ๊ธธ์ด ์„ค์ •ํ•˜๊ธฐ

- ๋ฐฐ์—ด์€ ๋ฐ”๋กœ ์˜†์— []๊ธฐํ˜ธ๋ฅผ ๋ถ™์ธ๋‹ค

- ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ๋จผ์ € ์„ค์ •ํ•œ ๋’ค, ๋ฐฐ์—ด ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ทธ ๊ฐ’์„ ๋‚˜์ค‘์— ๋Œ€์ž…

String[] weeks = new String[7];
weeks[0] = "์›”";
weeks[1] = "ํ™”";
weeks[2] = "์ˆ˜";
weeks[3] = "๋ชฉ";
weeks[4] = "๊ธˆ";
weeks[5] = "ํ† ";
weeks[6] = "์ผ";
// ์ดˆ๊นƒ๊ฐ’ ์—†์ด ๋ฐฐ์—ด ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค ๋•Œ์—๋Š” 7๊ณผ ๊ฐ™์€ ์ˆซ์žฃ๊ฐ’์„ ๋„ฃ์–ด ๊ธธ์ด๋ฅผ ์ •ํ•ด ์ค˜์•ผ ํ•œ๋‹ค...
// ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค...

 

๋ฐฐ์—ด๊ฐ’์— ์ ‘๊ทผํ•˜๊ธฐ

- ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ ์–ป์œผ๋ ค๋ฉด ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ์•ผ ํ•œ๋‹ค

String[] weeks = {"์›”", "ํ™”", "์ˆ˜", "๋ชฉ", "๊ธˆ", "ํ† ", "์ผ"}
System.out.println(week[3]); // ๋ชฉ

// weeks[3] => weeks๋ฐฐ์—ด์˜ ๋„ค๋ฒˆ์งธ ํ•ญ๋ชฉ์„ ์˜๋ฏธ(0๋ถ€ํ„ฐ ์ˆซ์ž๋ฅผ ์„ธ์ค€๋‹ค)

 

๋ฐฐ์—ด์˜ ๊ธธ์ด ๊ตฌํ•˜๊ธฐ

- ๋ฐฐ์—ด์€ ๋ณดํ†ต for ๋ฌธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ

- ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” length๋ฅผ ์‚ฌ์šฉ

String[] weeks = {"์›”", "ํ™”", "์ˆ˜", "๋ชฉ", "๊ธˆ", "ํ† ", "์ผ"};
// weeks.length = 7
for (int i = 0; i < weeks.length; i++) { 
	System.out.println(weeks[i])
} 
// ์›”
// ํ™”
// ์ˆ˜
// ๋ชฉ
// ๊ธˆ
// ํ† 
// ์ผ

 

๋ฐฐ์—ด ์˜ค๋ฅ˜

- ArrayIndexOutOfBoundsException : ๊ธธ์ด์™€ ์ธ๋ฑ์Šค ์‚ฌ์ด์—์„œ์ƒ๊ธฐ๋Š” ์˜ค๋ฅ˜...

   (ํŒŒ์ด์ฌ์˜ ์ธ๋ฑ์Šค์•„์›ƒ์˜ค๋ธŒ๋ ˆ์ธ์ง€ ๊ฐ™์€ ๋Š๋‚Œ..)

System.out.println(weeks[7]); 
// ์—ฌ๋Ÿ ๋ฒˆ์งธ ๋ฐฐ์—ด๊ฐ’์ด ์—†์œผ๋ฏ€๋กœ ArrayIndexOutOfBoundsException ์˜ค๋ฅ˜ ๋ฐœ์ƒ

 

7) ๋ฆฌ์ŠคํŠธ

: ๋ฐฐ์—ด๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ ํŽธ๋ฆฌํ•œ ๊ธฐ๋Šฅ์ด ๋” ๋งŽ์Œ...

๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” ์ •ํ•ด์ ธ ์žˆ์ง€๋งŒ ๋ฆฌ์ŠคํŠธ๋Š” ๋ณ€ํ•œ๋‹ค => ์ €์žฅํ•  ๊ฐ’์˜ ํฌ๊ธฐ๊ฐ€ ๋ช…ํ™•ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค!!

 

 

ArrayList

- ๋ฆฌ์ŠคํŠธ ์ž๋ฃŒํ˜•์—์„œ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ(๋ฆฌ์ŠคํŠธ ์ž๋ฃŒํ˜•์—๋Š” ArrayList, Vector, LinkedList ๋“ฑ์ด ์žˆ๋‹ค)

 

1. add ๋ฉ”์„œ๋“œ: ์š”์†Ÿ๊ฐ’ ์ถ”๊ฐ€

// ์•ผ๊ตฌ๊ณต์˜ ์‹œ์†์„ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•˜๋Š” ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ
import java.util.ArrayList;

public class Sample {
	public static void main(String[] args) {
    	ArrayList pitches = new ArrayList();
        pitches.add("138");
        pitches.add("129");
        pitches.add("142");
    }
}
// import ๋ฌธ : ๋‹ค๋ฅธ ํŒจํ‚ค์ง€๋‚˜ ํด๋ž˜์Šค๋ฅผ ํ˜„์žฌ ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์„ ์–ธํ•˜๋Š” ๋ฌธ์žฅ
// ArrayList๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด import java.util.ArrayList์™€ ๊ฐ™์€ import ๋ฌธ์„ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค
// Java.util ํŒจํ‚ค์ง€๋Š” ์ž๋ฐ”๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ํŒจํ‚ค์ง€์ด๋‹ค


// ์›ํ•˜๋Š” ์œ„์น˜์— ๋„ฃ๊ณ  ์‹ถ๋‹ค๋ฉด....
pitches.add(0, "133");  -> ์ฒซ๋ฒˆ์งธ ์œ„์น˜์— 133 ์‚ฝ์ž…
pitches.add(1, "133");  -> ๋‘๋ฒˆ์งธ ์œ„์น˜์— 133 ์‚ฝ์ž…

 

2. get ๋ฉ”์„œ๋“œ : ํŠน์ • ์ธ๋ฑ์Šค์˜ ๊ฐ’์„ ์ถ”์ถœ

import java.util.ArrayList;

public class Sample {
	public static void main(String[] args) {
    	ArrayList pitches = new ArrayList();
        pitches.add("138");
        pitches.add("129");
        pitches.add("142");
        # ์„ ์ˆ˜์˜ ํŠน์ • ํˆฌ๊ตฌ ์†๋„๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด!!!
        System.out.println(pitches.get(1));   // 129
    }
}

 

3. size ๋ฉ”์„œ๋“œ : ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฆฌํ„ด

System.out.println(pitches.size()); // 3

 

4. contains ๋ฉ”์„œ๋“œ : ๋ฆฌ์ŠคํŠธ ์•ˆ์— ํ•ด๋‹น ํ•ญ๋ชฉ์ด ์žˆ๋Š”์ง€ ํŒ๋ณ„ํ•ด ๊ทธ ๊ฒฐ๊ณผ๋ฅผ boolean์œผ๋กœ ๋ฆฌํ„ดํ•œ๋‹ค...

System.out.println(pitches.contains("142")); // true

 

5. remove ๋ฉ”์„œ๋“œ : 2๊ฐ€์ง€ ๋ฐฉ์‹์ด ์žˆ๋Š”๋ฐ... 1. remove(๊ฐ์ฒด), 2. remove(์ธ๋ฑ์Šค)

// remove(๊ฐ์ฒด)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ์ฒด์— ํ•ด๋‹นํ•˜๋Š” ํ•ญ๋ชฉ์„ ์‚ญ์ œ , ๊ทธ ๊ฒฐ๊ณผ๋กœ true ๋˜๋Š” false๋ฅผ ๋ฐ˜ํ™˜
System.out.println(pitches.remove("129")); // true

// remove(์ธ๋ฑ์Šค)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ธ๋ฑ์Šค์— ํ•ด๋‹นํ•˜๋Š” ํ•ญ๋ชฉ์„ ์‚ญ์ œํ•œ ๋’ค, ๊ทธ ํ•ญ๋ชฉ์„ ๋ฆฌํ„ด
System.out.println(pitches.remove(0)); // 138

 

์ œ๋„ค๋ฆญ์Šค(generics)

: ์ž๋ฃŒํ˜•์„ ์•ˆ์ „ํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์–ด ์ฃผ๋Š” ๊ธฐ๋Šฅ(์ž๋ฃŒํ˜•์„ ๊ฐ•์ œ๋กœ ๋ฐ”๊ฟ€ ๋•Œ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋Š” ์บ์ŠคํŒ… ์˜ค๋ฅ˜๋ฅผ ์ค„์ธ๋‹ค)

 

- ์ผ๋ฐ˜์ ์ธ ๋ฐฉ์‹ : ArrayList<String> pitches = new ArrayList<String>();

- ์„ ํ˜ธ๋˜๋Š” ๋ฐฉ์‹ : ArrayList<String> pirches = new ArrayList<>();  

=>  <>์— ํ•ด๋‹นํ•˜๋Š” ๋ถ€๋ถ„์˜ ์ž๋ฃŒํ˜•์€ ๋ช…ํ™•ํ•˜๋ฏ€๋กœ ๊ตณ์ด ์ ์ง€ ์•Š์•„๋„ ๋œ๋‹ค... (์˜คํžˆ๋ ค ์ ์œผ๋ฉด ์ธํ…”๋ฆฌ์ œ์ด์—์„œ๋Š” ๊ฒฝ๊ณ  ๋ฉ”์„ธ์ง€..)

 

- ์ œ๋„ค๋ฆญ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด... ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ ๋งค๋ฒˆ Object ์ž๋ฃŒํ˜•(๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ์ƒ์†ํ•˜๊ณ  ์žˆ๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์ž๋ฃŒํ˜•)์—์„œ String ์ž๋ฃŒํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•ด์•ผํ•œ๋‹ค.... 

 

// ์ œ๋„ค๋ฆญ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ์˜ˆ์‹œ
ArrayList pitches = new ArrayList();
pitches.add("138");
pitches.add("129");

// ํ˜• ๋ณ€ํ™˜์ด ํ•„์š”ํ•จ
String one = (String) pitches.get(0);
String two = (String) pirches.get(1);

// ์ œ๋„ค๋ฆญ์Šค๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์‹œ
ArrayList<String> pitches = new ArrayList<>();
pitches.add("138");
pitches.add("129");

// ํ˜•๋ณ€ํ™˜์ด ํ•„์š”์—†์Œ
String one = pitches.get(0)
String two = pitches.get(1)

 

๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ArrayList ๋งŒ๋“ค๊ธฐ

 

1. ArrayList์˜ add ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ : ArrayList ๊ฐ์ฒด์— ์š”์†Œ ์ถ”๊ฐ€

import java.util.AttayList;

public class Sample {
	public staatic void main(String[] args) {
    	ArrayList<String> pitches = new ArrayList<>(); // ์ œ๋„ค๋ฆญ์Šค๋ฅผ ์‚ฌ์šฉํ•œ ํ‘œํ˜„
        pitches.add("138");
        pitches.add("129");
        pitches.add("142");
        System.out.println(pitches); // [138, 129, 142]

 

2. ์ด๋ฏธ ๋ฌธ์ž์—ด ๋ฐฐ์—ด์ด ์žˆ๋‹ค๋ฉด...(java.util.Arrays ํด๋ž˜์Šค์˜ asList ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉ)

import java.util.ArrayList;
import java.util.Arrays;

public class Sample {
	public static void main(String[] args) {
    	String[] data = {"138", "129", "142"}; // ์ด๋ฏธ ํˆฌ๊ตฌ ์ˆ˜ ๋ฐ์ดํ„ฐ ๋ฐฐ์—ด์ด ์žˆ๋‹ค
        ArrayList<String> pitches = new ArrayList<>(Arrays.asList(data));
        System.out.println(pitches);  // [138, 129, 142]
    }
}

 

3. ๋ฌธ์ž์—ด ๋ฐฐ์—ด ๋Œ€์‹  ๋ฌธ์ž์—ด ์ž๋ฃŒํ˜•์„ ์—ฌ๋Ÿฌ๊ฐœ ์ „๋‹ฌํ•˜์—ฌ ์ƒ์„ฑํ•  ์ˆ˜๋„ ....

import java.util.ArrayList;
import java.util.Arrays;

public class Sample {
	public static void main(String[] args) {
        ArrayList<String> pitches = new ArrayList<>(Arrays.asList("138", "129", "142"));
        System.out.println(pitches);  // [138, 129, 142]
    }
}

 

String.join("๊ตฌ๋ถ„์ž", ๋ฆฌ์ŠคํŠธ ๊ฐ์ฒด)

: ArrayList์˜ ๊ฐ ์š”์†Œ๋ฅผ ๋ฌด์–ธ๊ฐ€๋กœ(์ฝค๋งˆ(,))๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ 1๊ฐœ์˜ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค๊ธฐ...

 

1) join์ด ์—†๋‹ค๋ฉด... ์ฝค๋งˆ๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•˜์—ฌ ๋งˆ์ง€๋ง‰์— ์ฝค๋งˆ๋Š” ์ œ๊ฑฐํ•œ ํ›„ ์ถœ๋ ฅ

2) join์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ• ์ˆ˜ ์žˆ๋‹ค!!

import java.util.ArrayList;
import java.util.Arrays;

public class Sample {
	public static void main(String[] args) {
    	ArrayList<String> pitches = new ArrayList<>(Arrays.asList("138", "129", "142"));
        String result = String.join(",", pirches);
        System.out.println(result); // "138, 129, 142"
    }
}

 

๋ฆฌ์ŠคํŠธ ์ •๋ ฌํ•˜๊ธฐ

: ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฆฌ์ŠคํŠธ์˜ sort ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

public class Sample {
	public static void main(String[] args) {
    	ArrayList<String> pitches = new ArrayList<>(Arrays.asList("138", "129", "142"));
        pitches.sort(Comparator.naturalOrder()); // ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
        System.out.println(pitches); // [129, 138, 142]
    }
}

- ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ : Comparator.naturalOrder()

- ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ : Comparator.reverseOrder()

 

 


 

8) ๋งต

- ๋”•์…”๋„ˆ๋ฆฌ์™€ ์œ ์‚ฌํ•˜๊ณ   ํ‚ค์™€ ๊ฐ’์„ ํ•œ ์Œ์œผ๋กœ ๊ฐ–๋Š” ์ž๋ฃŒํ˜•

- ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์ˆœ์ฐจ์ ์œผ๋กœ ์š”์†Ÿ๊ฐ’์„ ๊ตฌํ•˜์ง€ ์•Š๊ณ  ํ‚ค๋ฅผ ์ด์šฉํ•ด ๊ฐ’์„ ์–ป๋Š”๋‹ค

- HashMap, LinkedHashMap, TreeMap ๋“ฑ์ด ์žˆ๋‹ค...

 

HashMap : ๋งต ์ž๋ฃŒํ˜•์˜ ๊ฐ€์žฅ ๊ธฐ๋ณธ

1. put ๋ฉ”์„œ๋“œ : key์™€ value๋ฅผ ์ถ”๊ฐ€

import java.util.HashMap;
public class Sample {
	public static void main(String[] args) {
    	HashMap<String, String> map = new HashMap<>();
        map.put("people", "์‚ฌ๋žŒ");
        map.put("baseball", "์•ผ๊ตฌ");
    }
}
// key, value์— String ์ด์™ธ์˜ ์ž๋ฃŒํ˜•์€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค

 

2. get ๋ฉ”์„œ๋“œ : key์— ํ•ด๋‹นํ•˜๋Š” value๋ฅผ ์–ป์Œ

import java.util.HashMap;
public class Sample {
	public static void main(String[] args) {
    	HashMap<String, String> map = new HashMap<>();
        map.put("people", "์‚ฌ๋žŒ");
        map.put("baseball", "์•ผ๊ตฌ");
        System.out.println(map.get("people")); // ์‚ฌ๋žŒ
    }
}

// ๋งต์˜ key์— ํ•ด๋‹นํ•˜๋Š” value๊ฐ€ ์—†์„ ๋–„ get ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด null์ด ๋ฆฌํ„ด
System.out.println(map.get("java")); // null

// null ๋Œ€์‹  ๊ธฐ๋ณธ๊ฐ’์„ ์–ป๊ณ  ์‹ถ๋‹ค๋ฉด getOrDefault ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค
System.out.println(map.getOrDefault("java", "์ž๋ฐ”")); //์ž๋ฐ”

 

3. containsKey ๋ฉ”์„œ๋“œ : ๋งต์— ํ•ด๋‹น key๊ฐ€ ์žˆ๋Š”์ง€๋ฅผ true ๋˜๋Š” false๋กœ ๋ฆฌํ„ด

HashMap<String, String> map = new HashMap<>();
map.put("people", "์‚ฌ๋žŒ");
map.put("baseball", "์•ผ๊ตฌ");
System.out.println(map.containsKey("people")); // true

 

4. remove ๋ฉ”์„œ๋“œ : ๋งต์˜ ํ•ญ๋ชฉ์„ ์‚ญ์ œํ•˜๋Š” ๋ฉ”์„œ๋“œ๋กœ, ํ•ด๋‹น key์˜ ํ•ญ๋ชฉ์„ ์‚ญ์ œํ•œ ํ›„ value ๊ฐ’์„ ๋ฆฌํ„ด

HashMap<String, String> map = new HashMap<>();
map.put("people", "์‚ฌ๋žŒ");
map.put("baseball", "์•ผ๊ตฌ");
System.out.println(map.remove("people")); // ์‚ฌ๋žŒ

 

5. size ๋ฉ”์„œ๋“œ : ๋งต ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฆฌํ„ด

HashMap<String, String> map = new HashMap<>();
map.put("people", "์‚ฌ๋žŒ");
map.put("baseball", "์•ผ๊ตฌ");
System.out.println(map.containsKey("people")); 
System.out.println(map.size()); // 1

 

6. keySet ๋ฉ”์„œ๋“œ : ๋งต์˜ ๋ชจ๋“  key๋ฅผ ๋ชจ์•„์„œ ๋ฆฌํ„ด

import java.util.HashMap;

public class Sample {
	public static void main(String[] args) {
    	HashMap<String, String> map = new HashMap<>();
        map.put("people", "์‚ฌ๋žŒ");
        map.put("baseball", "์•ผ๊ตฌ");
        System.our.println(map.keySet()); // [baseball, people]
    }
}

 

++ ) ๋งต์˜ ๊ฐ€์žฅ ํฐ ํŠน์ง•์€ ์ˆœ์„œ์— ์˜์กดํ•˜์ง€ ์•Š๊ณ  key๋กœ value ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š”๊ฒƒ์ด์ง€๋งŒ... ๊ฐ€๋” map์— ์ž…๋ ฅ๋œ ์ˆœ์„œ๋Œ€๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์ž…๋ ฅํ•œ key์— ์˜ํ•ด ์ •๋ ฌํ•˜๋„๋ก ์ €์žฅํ•˜๊ณ  ์‹ถ์„ ์ˆ˜ ์žˆ๋‹ค...

 

- LinkedHashMap : ์ž…๋ ฅ๋œ ์ˆœ์„œ๋Œ€๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ

- TreeMap : ์ž…๋ ฅ๋œ key์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ

 


9) ์ง‘ํ•ฉ

: ์ง‘ํ•ฉ๊ณผ ๊ด€๋ จ๋œ ๊ฒƒ์„ ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ ๊ฒƒ์œผ๋กœ... HashSet, TreeSet, LinkedHashSet ๋“ฑ์ด ์žˆ๋‹ค...

 

- ํŠน์ง• 2๊ฐ€์ง€...

1. ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค

2, ์ˆœ์„œ๊ฐ€ ์—†๋‹ค(๋ฆฌ์ŠคํŠธ์™€ ๋ฐฐ์—ด์€ ์ˆœ์„œ๊ฐ€ ์žˆ์ฃ ..., ์ˆœ์„œ๊ฐ€ ์—†๋Š” ์ž๋ฃŒํ˜•์ด๋ผ ์ธ๋ฑ์‹ฑ์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค)

 

 

๊ต์ง‘ํ•ฉ, ํ•ฉ์ง‘ํ•ฉ, ์ฐจ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ

1. ์ง‘ํ•ฉ ์ž๋ฃŒํ˜• 2๊ฐœ ๋งŒ๋“ค๊ธฐ

2. ๊ต์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ

3. ํ•ฉ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ

4. ์ฐจ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ

import java.util.Arrays;
import java.util.HashSet;
 
 public class Sample {
 	public static void main(Stirng[] args) {
    	// ์ง‘ํ•ฉ ์ž๋ฃŒํ˜• ๋‘๊ฐœ ๋งŒ๋“ค๊ธฐ 
    	HashSet<Integer> s1 = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5, 6));
        HashSet<Integer> s2 = new HashSet<>(Arrays.asList(4, 5, 6, 7, 8, 9));
        
        // (๊ต์ง‘ํ•ฉ) = retainAll ๋ฉ”์„œ๋“œ ์ด์šฉ
        HashSet<Integer> intersection = new HashSet<>(s1); // s1์œผ๋กœ intersection ์ด๋ผ๋Š” HashSet ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌํ•ด ์ƒ์„ฑ
        intersection.retainAll(s2); // ๊ต์ง‘ํ•ฉ ์ˆ˜ํ–‰
        System.out.println(intersection); // [4, 5, 6]
        
        // (ํ•ฉ์ง‘ํ•ฉ) = addAll ๋ฉ”์„œ๋“œ ์ด์šฉ
        HashSet<Integer> union = new HashSet<>(s1); // s1์œผ๋กœ union ์ƒ์„ฑ
        union.addAll(s2); // ํ•ฉ์ง‘ํ•ฉ ์ˆ˜ํ–‰
        System.out.println(union);  // [1, 2, 3, 4, 5, 6, 7, 8, 9]
        
        // (์ฐจ์ง‘ํ•ฉ) = removeAll ๋ฉ”์„œ๋“œ ์ด์šฉ
        HashSet<Integer> substract = new HashSet<>(s1); // s1์œผ๋กœ substract ์ƒ์„ฑ
        substract.removeAll(s2); // ์ฐจ์ง‘ํ•ฉ ์ˆ˜ํ–‰
        System.out.println(substract); // [1, 2, 3]
    }
}

 

์ง‘ํ•ฉ ์ž๋ฃŒํ˜•๊ณผ ๊ด€๋ จ๋œ ๋ฉ”์„œ๋“œ - add, addAll, remove

1. add: ์ง‘ํ•ฉ ์ž๋ฃŒํ˜•์— ๊ฐ’์„ ์ถ”๊ฐ€

import java.util.HashSet;

public class Sample {
	public static void main(String[] args) {
    	HashSet<Stirng> set = new HashSet<>();
        set.add("Jump");
        set.add("To");
        set.add("Java");
        System.our.println(set); // [Java, To, Jump]
    }
}

 

2. addAll : ํ•œ๊บผ๋ฒˆ์— ์—ฌ๋Ÿฌ๊ฐœ ๊ฐ’์„ ์ถ”๊ฐ€

import java.util.HashSet;

public class Sample {
	public static void main(String[] args) {
    	HashSet<Stirng> set = new HashSet<>();
        set.add("Jump");
        set.add("To");
        set.addAll(Arrays.asList("To", "Java"));
        System.our.println(set); // [Java, To, Jump]
    }
}

 

3. remove : ํŠน์ • ๊ฐ’์„ ์ œ๊ฑฐํ•  ๋–„ ์‚ฌ์šฉ

import java.util.HashSet;

public class Sample {
	public static void main(String[] args) {
    	HashSet<Stirng> set = new HashSet<>(Arrays.asList("Jump", "To", "Java"));
        set.add("To");
        System.our.println(set); // [Java, Jump]
    }
}

 


 

10) ์ƒ์ˆ˜ ์ง‘ํ•ฉ

 : enum ์ž๋ฃŒํ˜•์€ ์„œ๋กœ ์—ฐ๊ด€ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ƒ์ˆ˜ ์ง‘ํ•ฉ์„ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉ

public class Sample {
	enum CoffesType {
    	AMERICANO,
        ICE_AMERICANO,
        CAFE_LATTE
    };
    
    public static void main(String[] args) {
    	System.out.println(CoffeeType.AMERICANO);  // AMERICANO
        System.out.println(CoffeeType.ICE_AMERICANO);  // ICE_AMERICANO
        System.out.println(CoffeeType.CAGE_LATTE);   // CAFE_LATTE
    }
}


// ๋ฐ˜๋ณต๋ฌธ์—์„œ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
public class Sample {
	enum CoffesType {
    	AMERICANO,
        ICE_AMERICANO,
        CAFE_LATTE
    };
    
     public static void main(String[] args) {
     	for(CoffeeType type: CoffeeType.values()) {
        	System.out.println(type);  // ์ˆœ์„œ๋Œ€๋กœ AMERICANO, ICE_AMERICANO, CAFE_LATTE ์ถœ๋ ฅ
        }
    }
}

- enum ์‚ฌ์šฉ์˜ ์žฅ์ 

1. ๋งค์ง ๋„˜๋ฒ„๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋ณด๋‹ค ์ฝ”๋“œ๊ฐ€ ๋ช…ํ™•ํ•˜๋‹ค

2. ์ž˜๋ชป๋œ ๊ฐ’์„ ์‚ฌ์šฉํ•ด ์ƒ๊ธธ ์ˆ˜ ์žˆ๋Š” ์˜ค๋ฅ˜๋ฅผ ๋ง‰์„ ์ˆ˜ ์žˆ๋‹ค

 

 


11) ํ˜• ๋ณ€ํ™˜๊ณผ final

: ํ˜• ๋ณ€ํ™˜์ด๋ž€ ์ž๋ฃŒํ˜•์„ ๋‹ค๋ฅธ ์ž๋ฃŒํ˜•์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ ("123"๊ณผ ๊ฐ™์€ ๋ฌธ์ž์—ด์„ 123๊ณผ ๊ฐ™์€ ์ˆซ์žํ˜•์œผ๋กœ)

 

ํ˜•๋ณ€ํ™˜ 

1.  ๋ฌธ์ž์—ด์„ ์ •์ˆ˜๋กœ

String num = "123";
//๋ฌธ์ž์—ด์„ ์ •์ˆ˜๋กœ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด์„œ๋Š” Integer ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค

public class Sample {
	public static void main(String[] args) {
    	String num = "123";
        int n = Integer.parseInt(num);
        System.out.println(n); 			// 123
    }
}

 

2. ์ •์ˆ˜๋ฅผ ๋ฌธ์ž์—ด๋กœ : ์ •์ˆ˜ ์•ž์— ๋นˆ ๋ฌธ์ž์—ด์„ ๋”ํ•ด ์ฃผ๊ธฐ 

public class Sample {
	public static void main(String[] args) {
    	int n = 123;
        String num = "" + n;
        System.out.println(num);  // 123
    }
}

 

 ++)  String.valueOf(์ •์ˆ˜), Integer.toString(์ •์ˆ˜) ๋ชจ๋‘ ์ •์ˆ˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พผ๋‹ค....

public class Sample {
	public static void main(String[] args) {
    	int n = 123;
        String num1 = String.valueOf(n);
        String num2 = Integer.toString(n);
        System.out.println(num1);   // 123
        System.out.println(num2);   // 123
    }
}

 

3. ์†Œ์ˆ˜์ ์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š” ์ˆซ์ž ํ˜•ํƒœ์˜ ๋ฌธ์ž์—ด์˜ ๋ณ€ํ™˜

public class Sample {
	public static void main(String[] args) {
    	String num = "123.456";
        double d = Double.parseDouble(num);
        System.out.println(d);    // 123.456
    }
}

 

4. ์ •์ˆ˜์™€ ์‹ค์ˆ˜ ์‚ฌ์ด์˜ ํ˜• ๋ณ€ํ™˜

public class Sample {
	public static void main(String[] args) {
    	int n1 = 123;
        double d1 = n1;
        System.out.println(d1);  // 123.0
        
        double d2 = 123.456;
        int n2 = (int) d2;
        System.out.println(n2);  // 123
    }
}

 

5. ์‹ค์ˆ˜ ํ˜•ํƒœ์˜ ๋ฌธ์ž์—ด์„ ์ •์ˆ˜๋กœ ๋ฐ”๊ฟ€ ๋•Œ

public class Sample {
	public static void main(String[] args) {
    	String num = "123.456";
        int n = Integer.parseInt(num);  // ์‹ค์ˆ˜ ํ˜•ํƒœ์˜ ๋ฌธ์ž์—ด์„ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•  ๊ฒฝ์šฐ numberformatException์ด ๋ฐœ์ƒ
    }
}

// ์‹ค์ˆ˜ ํ˜•ํƒœ์˜ ๋ฌธ์ž์—ด์„ ์ˆซ์žํ˜•์œผ๋กœ ๋ฐ”๊พธ๋ ค๋ฉด... Double.parseDouble()์„ ์ด์šฉํ•ด ์‹ค์ˆ˜๋กœ ๋ณ€ํ™˜ ํ›„ ์‚ฌ์šฉ

 

final

: ์ž๋ฃŒํ˜•์—์„œ ๊ฐ’์„ ๋‹จ ํ•œ ๋ฒˆ๋งŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๊ฐ•์ œํ•˜๋Š” ํ‚ค์›Œ๋“œ (๋‹ค์‹œ ์„ค์ • ์•ˆ๋ฉ๋‹ˆ๋‹ค)

public class Sample {
	public static void main(String[] args) {
    	final int n = 123;   // final๋กœ ์„ค์ •ํ•˜๋ฉด ๊ฐ’์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๋‹ค
        n = 456;
    }
}

 

๋ฆฌ์ŠคํŠธ์˜ ๊ฒฝ์šฐ๋„ final๋กœ ์„ ์–ธํ•˜๋ฉด ์žฌํ• ๋‹น์€ ๋ถˆ๊ฐ€๋Šฅ...(๋”ํ•˜๊ฑฐ๋‚˜ ๋บด๊ธฐ๋Š” ๊ฐ€๋Šฅ)

import java.util.ArrayList;
import java.util.Arrays;

public class Sample {
	public static void main(String[] args) {
    	final ArrayList<String> a = new ArrayList<>(Arrays.asList("a", "b"));
        a = new ArrayList<>(Arrays,asList("c", "d")); // ์ปดํŒŒ์ผ ์˜ค๋ฅ˜ ๋ฐœ์ƒ
    }
}

// ๊ทธ๋Ÿฌ๋ฏ€๋กœ final์€ ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด์„œ ๊ทธ ๊ฐ’์ด ๋ฐ”๋€Œ๋ฉด ์•ˆ ๋  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค

 

๋ฆฌ์ŠคํŠธ์˜ ์žฌํ• ๋‹น ๋ฟ๋งŒ์•„๋‹ˆ๋ผ ๋”ํ•˜๊ฑฐ๋‚˜ ๋นผ๋Š” ๊ฒƒ๋„ ํ•  ์ˆ˜ ์—†๊ฒŒ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด... List.of๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋Š” ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค!!!

import java.util.List;

public class Sample {
	public static void main(String[] args) {
    	final List<String> a = List.of("a", "b");
        a.add("c"); // UnsupportedOperationException ๋ฐœ์ƒ
    }
}

 

++) ๋ฌธ์ œ ํ’€์ด

 

6. myList.sort(Comparator,reverseOrder());

7. String result = String.join(" ", myList);

8. int result = grade.remove("B")

9. ArrayList<Integer> result = new ArratList<>(temp);

10. 
public class Sample{
	enum CoffeeType {
    	AMERICANO,
        ICE_AMERICANO,
        CAFE_LATTE
    };
    
 ...
 
 printCoffeePrice(CoffeeType,AMERICANO);

 

728x90
๋ฐ˜์‘ํ˜•

+ Recent posts