-
๋ค์ฐจ์ ๋ฐฐ์ด, ArrayListJava 2022. 2. 28. 22:28
๐ก 2์ฐจ์ ์ด์์ ๋ฐฐ์ด
: ์๋ฃํ[][] ๋ฐฐ์ด์ด๋ฆ = new ์๋ฃํ[ํ ๊ฐ์][์ด ๊ฐ์];
: int[][] arr = {{1,2},{3,4}}; => ์ ์ธ&์ด๊ธฐํ
: for๋ฌธ(์ด์คfor๋ฌธ) => ํ์ ๊ธฐ์ค์ผ๋ก ์ก๊ณ -> ์ด์ ๋๋ฆฐ๋ค
public class Array { public static void main(String[] args) { // ๋ค์ฐจ์ ๋ฐฐ์ด int[][] num = {{1,2},{2,3},{3,4}}; for(int i=0; i<num.length; i++) { // ํ์ ๊ธธ์ด for(int j=0; j<num[i].length; j++) { // ์ด์ ๊ธธ์ด System.out.println(num[i][j]); } System.out.println(); } } }
: ๊ฒฐ๊ณผ
๐ก ArrayList ํด๋์ค
: ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉํ๋ ๊ฐ์ฒด ๋ฐฐ์ด ํด๋์ค <- ๋ช ํํ ํฌ๊ธฐ๋ฅผ ์ ์ ์์๋ ์ฌ์ฉ
* ๋ฐฐ์ด์ ์ ์ธ์ ํฌ๊ธฐ๋ฅผ ์ง์ , ArrayList๋ ํฌ๊ธฐ๊ฐ ๋์ ์ผ๋ก ๋ณํ๋ค, ์ํ๋ ๋งํผ ๋ด์ ์ ์๋ค
: ArrayList<๊ฐ์ฒด ํ์ > ๊ฐ์ฒด๋ช = new ArrayList<๊ฐ์ฒด ํ์ >();
โ add("๊ฐ") : ์ถ๊ฐ,์ ๋ ฅ, add(์์น, "๊ฐ") : ์์น๋ ์ง์ ๊ฐ๋ฅ
โ get(์ธ๋ฑ์ค) : ์ถ๋ ฅ
โ size() : ArrayList์ ๊ฐฏ์ ๋ฆฌํด
โ contains("๊ฐ") : ๋ฆฌ์คํธ ์์ ํด๋น ํญ๋ชฉ์ด ์กด์ฌํ๋์ง ํ๋ณ -> boolean์ผ๋ก ๋ฆฌํด
โ remove() : ์ญ์
+ remove(๊ฐ์ฒด) : ํด๋นํ๋ ๊ฐ ์ญ์ -> ๊ฒฐ๊ณผ(true/false) ๋ฆฌํด
+ remove(์ธ๋ฑ์ค) : ํด๋น ์์น ๊ฐ ์ญ์ -> ๊ฒฐ๊ณผ(์ธ๋ฑ์ค์ ํด๋นํ๋ ๊ฐ) ๋ฆฌํด
โ String.join
: String.join("๊ตฌ๋ถ์", ๋ฆฌ์คํธ๊ฐ์ฒด), ์ผ๋ฐ ๋ฐฐ์ด์๋ ์ฌ์ฉ๊ฐ๋ฅ
ex) 138,129,142 <-์ ๊ฐ์ด ๋ฐฐ์ด ์ฌ์ด์ , ๋ฃ๊ธฐ
String result = String.join("," , ๋ฆฌ์คํธ๊ฐ์ฒด);
โ sort() : ๋ฆฌ์คํธ ์ ๋ ฌ
: ๋ฆฌ์คํธ๊ฐ์ฒด.sort(Comparator.naturalOrder());
์ค๋ฆ์ฐจ์(์๋ฐฉํฅ) ์ ๋ ฌ - Comparator.naturalOrder()
๋ด๋ฆผ์ฐจ์(์ญ๋ฐฉํฅ) ์ ๋ ฌ - Comparator.reverseOrder() ๋์คํ๋ ์ ๋ ฅ
๐ ์ ๋ค๋ฆญ์ค (Generics)
ex) ArrayList<String> list = new ArrayList<>();
== ArrayList ์์ ๋ด์ ์ ์๋ ์๋ฃํ์ Stringํ์ ๋ฟ์ด๋ค
: ์ด๋ ๊ฒ ๋ช ํํ๊ฒ ์์ฑํด์ฃผ๋๊ฒ์ด ์ข๋ค
* int๋ฅผ ์ ๋ค๋ฆญ์ค๋ก ์ฌ์ฉํ๋ ค๋ฉด Integer๋ก ์ฌ์ฉํด์ผ ํ๋ค
๐ ์ด๋ฏธ ์กด์ฌํ๋ ๋ฐฐ์ด -> ArrayList
import java.util.ArrayList; import java.util.Arrays; public class Main { public static void main(String[] args) { String[] str = {"a","b","c"}; ArrayList<String> list = new ArrayList<>(Arrays.asList(str)); System.out.println(list); // [a,b,c] ์ถ๋ ฅ } }
๐ ArrayList ์ด์ฉํด ํ์๋ค์ ๊ณผ๋ชฉ ์ ์ ํ์ธํ๊ธฐ
package array; public class Subject { private String title; private int score; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } }
package array; import java.util.ArrayList; public class Student { private int studentID; private String studentName; private ArrayList<Subject> subjectList ; // ์๊ฐํ ์ฌ๋ฌ๊ฐ์ ๊ณผ๋ชฉ public Student(int studentID, String studentName) { // ํ์์ ๋ํ์ฌ ์์ฑ this.studentID = studentID; this.studentName = studentName; subjectList = new ArrayList<Subject>(); // ํ์์ด ์์ฑ๋๋ฉด -> ๋ฐฐ์ด๋ ์์ฑ๋์ผํจ, ArrayList๋ Subject์์๋ฅผ ์ง๋๋ค } public void addSubject(String title, int score) { // ๊ณผ๋ชฉ ์ถ๊ฐ Subject subject = new Subject(); subject.setTitle(title); subject.setScore(score); subjectList.add(subject); } public void showStudentInfo() { // ์ ๋ณด ์ถ๋ ฅ for(Subject subject : subjectList) { System.out.println(studentName + "ํ์์ "+ subject.getTitle() + "๊ณผ๋ชฉ์ ์ฑ์ ์ " + subject.getScore() + "์ ๋๋ค"); } } }
package array; import java.util.ArrayList; public class Array { public static void main(String[] args) { Student studentJ = new Student(1, "james"); studentJ.addSubject("๊ตญ์ด", 90); studentJ.addSubject("์์ด", 95); studentJ.addSubject("์ํ", 80); studentJ.showStudentInfo(); System.out.println("======================="); Student studentT = new Student(2, "tomas"); studentT.addSubject("๊ตญ์ด", 80); studentT.addSubject("์์ด", 90); studentT.addSubject("์ํ", 100); studentT.showStudentInfo(); } }
: ๊ฒฐ๊ณผ
* ๋ฉค๋ฒ ๋ณ์๋ ํ์ํ ๊ฒ ์๋๋ฉด private๋ก, ๋์ค์ ํ์ํ ๋ ๋ณ๊ฒฝ
'Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ถ์ ํด๋์ค (0) 2022.03.04 ์์, ๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋ฉ, ๊ฐ์ ๋ฉ์๋, ๋คํ์ฑ (0) 2022.03.01 ๋ฐฐ์ด, ํฅ์๋ for๋ฌธ (0) 2022.02.28 ํจํค์ง, ์ ๊ทผ ์ ์ด์, static ๋ณ์, singleton ํจํด (0) 2022.02.28 ์์ฑ์, this, ๊ฐ์ฒด ๊ฐ์ ํ๋ ฅ (0) 2022.02.22