๋ฌธ์ 1.
- ํน์ ๋ฌธ์์ด์ด ์ฃผ์ด์ก์ ๋, ๋ฌธ์์ด์ ์ฌ์ฉ๋ ๋ฌธ์์ ์๋ฅผ ๊ตฌํ์์ค
- ๋์ด์ฐ๊ธฐ๋ ํฌํจํ์ง ์์ผ๋ฉฐ, ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ถํฉ๋๋ค
- ์์ : tetz → 3
- t ๋ ์ค๋ณต์ด๋ฏ๋ก 1๊ฐ๋ง ๊ณ์ฐ → t, e, z ๊ฐ ์ฌ์ฉ ๋์์ผ๋ฏ๋ก ๋ฌธ์์ ์๋ 3๊ฐ
- ์์ : Tetz is old
- ๋์ด์ฐ๊ธฐ๋ฅผ ์ ์ธํ๊ณ T, e, t, z, i, s, o, l, d ๊ฐ ์ฌ์ฉ ๋์์ผ๋ฏ๋ก ์ ๋ต์ 9
import java.util.ArrayList;
import java.util.Scanner;
public class HowManyChar {
public static void main(String[] args) {
// ์ฌ์ฉ์ ์
๋ ฅ ๋ฐ๊ธฐ
Scanner scanner = new Scanner(System.in);
System.out.println("๋ฌธ์์ด์ ์
๋ ฅํ์ธ์.");
String input = scanner.nextLine();
//๊ณต๋ฐฑ์ ๊ฑฐ
input = input.replace(" ". "");
// ์ฌ์ฉ๋ ๋ฌธ์ ์ ์ฅํ ArrayList ์์ฑ
ArrayList<Character> uniqueChars = new ArrayList<>();
// ๋ฌธ์์ด ์ํ
for (int i = 0; i < input.length(); i++) {
char currentChar = input.charAt(i);
//ArrayList์ ์๋ ๋ฌธ์๋ง ์ถ๊ฐ
if (!uniqueChars.contains(currentChar)) {
uniqueChars.add(currentChar);
}
}
//System.out.println("์ฌ์ฉ๋ ๋ฌธ์์ ์: " + uniqueChars. size());
}
}
<๋๊ตฌ ์ค๋ช >
1. input.replace(" ", "") : ์ ๋ ฅ ๋ฌธ์์ด์์ ๊ณต๋ฐฑ์ ์ ๊ฑฐ.
2. input.charAt(i) : ์๋ฐ์์ ๋ฌธ์์ด(String)์ ํน์ ์์น์ ์๋ ๋ฌธ์๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฉ์๋.
3. ArrayList<Character> : ๊ณ ์ ๋ฌธ์๋ฅผ ์ ์ฅํ๋ ๋ฆฌ์คํธ. ์ค๋ณต๋ ๋ฌธ์๋ ์ ์ฅ๋์ง ์๋๋ค.
4. if (!uniqueChars.contains(currentChar)) :ํ์ฌ ๋ฌธ์๊ฐ ๋ฆฌ์คํธ์ ์๋ ๊ฒฝ์ฐ์๋ง ์ถ๊ฐํ๋ค.
5. ๊ฒฐ๊ณผ ์ถ๋ ฅ : uniqueChars.size()๋ฅผ ํธ์ถํ์ฌ ๋ฆฌ์คํธ์ ํฌ๊ธฐ, ์ฆ ๊ณ ์ ๋ฌธ์์ ๊ฐ์๋ฅผ ์ถ๋ ฅํ๋ค.
<๋ ผ๋ฆฌ์ ํ๋ฆ ์ค๋ช >
1. ์ ๋ ฅ ๋ฌธ์์ด ์ฒ๋ฆฌ
• ๋ฌธ์์ด์์ ๊ณต๋ฐฑ(' ')์ ์ ๊ฑฐํ๋ค.
replace ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ํํ๋ค.
2. ์ค๋ณต ๋ฌธ์ ํ์ธ ๋ฐ ์ ์ฅ
• ArrayList๋ฅผ ์ด์ฉํ์ฌ ์ฌ์ฉ๋ ๋ฌธ์๋ฅผ ์ ์ฅํ๋ค.
• ๋ฌธ์์ด์ ๊ฐ ๋ฌธ์๋ฅผ ์ํํ๋ฉฐ, ํด๋น ๋ฌธ์๊ฐ ArrayList์ ์๋ ๊ฒฝ์ฐ์๋ง ์ถ๊ฐํ๋ค.
3. ๊ฒฐ๊ณผ ๊ณ์ฐ
• ArrayList์ ํฌ๊ธฐ๋ฅผ ํ์ธํ์ฌ ๋ฌธ์์ด์ ์ฌ์ฉ๋ ๊ณ ์ ๋ฌธ์์ ์๋ฅผ ๋ฐํํ๋ค.
4. ์ฝ๋ ์์ฑ
• Scanner๋ฅผ ์ฌ์ฉํด ์ฌ์ฉ์ ์ ๋ ฅ์ ๋ฐ์ ์ฒ๋ฆฌํ๋ค.
• ์์์ ์ค๋ช ํ ๋ก์ง์ ๊ตฌํํ์ฌ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ค.
๋ฌธ์ 2.
์๊ตฌ์ฌํญ
1. Student ํด๋์ค๋ฅผ ์์ฑํ์ฌ ๋ฉค๋ฒ ๋ณ์(name๊ณผ address)๋ฅผ ๊ฐ์ง๋๋ก ์ค๊ณํ๊ธฐ.
2 . Student ํด๋์ค๋ ๋ค์ ๊ธฐ๋ฅ์ ํฌํจํด์ผ ํฉ๋๋ค:
• Getter/Setter ๋ฉ์๋: ๊ฐ์ฒด์ name๊ณผ address ๊ฐ์ ์ฝ๊ณ ์์ ํ ์ ์๋๋ก ๊ตฌํ.
•.toString() ๋ฉ์๋: ๊ฐ์ฒด์ ์ ๋ณด๋ฅผ ๋ณด๊ธฐ ์ข๊ฒ ์ถ๋ ฅํ ์ ์๋๋ก ๋ฌธ์์ด ํํ๋ก ๋ฐํ.
•.main ๋ฉ์๋์์ ArrayList์ Student ๊ฐ์ฒด๋ฅผ ์ถ๊ฐํ๊ธฐ.
• solution ๋ฉ์๋์์ ๋ฆฌ์คํธ๋ฅผ ์ํํ๋ฉฐ ๊ฐ Student ๊ฐ์ฒด๋ฅผ ์ถ๋ ฅํ๊ธฐ.
import java.util.ArrayList;
import java.util.List;
//Student ํด๋์ค ์ ์
class Student {
private String name;
private String address;
//์์ฑ์
public Student (String name, String address) {
this.name = name;
this.address = address;
}
//Getter ๋ฉ์๋
public String getName() {
return name;
}
public String getAddress() {
return address;
}
// Setter ๋ฉ์๋
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
// toString ๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋
@Override
public String toString() {
return "Student{name='" + name + "', address='" + address + "'}";
}
}
public class ListAssignment2 {
// solution ๋ฉ์๋ ๊ตฌํ
public static void solution(List<Student> studentList) {
// Student ๊ฐ์ฒด ์ถ๋ ฅ
for (Student student : studentList) {
System.out.println(student);
}
}
public static void main(String[] args) {
// Student ๊ฐ์ฒด ๋ฆฌ์คํธ ์์ฑ
List<Student> studentList = new ArrayList<>();
// Student ๊ฐ์ฒด ์ถ๊ฐ
studentList.add(new Student("๊น์ฐ๋น", "00์ฃผ"));
studentList.add(new Student("์์์", "00๊ตฌ"));
studentList.add(new Student("์์์", "00๊ตฌ"));
// solution ๋ฉ์๋ ํธ์ถ
solution(studentList);
}
}
}
1. Student ํด๋์ค ์ ์
• name๊ณผ address๋ฅผ ๋ฉค๋ฒ ๋ณ์๋ก ์ ์ธ.
• ์์ฑ์, Getter, Setter ๋ฉ์๋, toString ๋ฉ์๋๋ฅผ ๊ตฌํ.
2. solution ๋ฉ์๋
• List<Student>๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ ๊ฐ Student ๊ฐ์ฒด๋ฅผ ์ถ๋ ฅ.
3. main ๋ฉ์๋
• ArrayList๋ฅผ ์์ฑํ๊ณ , Student ๊ฐ์ฒด๋ฅผ ์ถ๊ฐ.
• solution ๋ฉ์๋์ ๋ฆฌ์คํธ๋ฅผ ์ ๋ฌํ์ฌ ๊ฒฐ๊ณผ ์ถ๋ ฅ.