๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[JAVA ์ฝ๋ฉ ์ด๋ณด] List (๋ฆฌ์คํธ) Method ๋ณธ๋ฌธ
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
๋ฐ์ํ
'๐ฆฅ ์ฝํ > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA ์ฝ๋ฉ ์ด๋ณด] Queue (ํ) Method (0) | 2023.07.16 |
---|---|
[JAVA ์ฝ๋ฉ ์ด๋ณด] Stack (๋ฆฌ์คํธ) Method (0) | 2023.07.16 |
[JAVA ์ฝ๋ฉ ์ด๋ณด] StringBuilder(์คํธ๋ง๋น๋) Method (0) | 2023.07.16 |
[JAVA ์ฝ๋ฉ ์ด๋ณด] String(์คํธ๋ง) Method (0) | 2023.07.16 |
[JAVA ์ฝ๋ฉ ์ด๋ณด] ๋ฐฐ์ด ์ ๋ ฌ (์ค๋ฆ์ฐจ์, ๋ด๋ฆผ์ฐจ์) (0) | 2023.07.16 |
Comments