๋ฌธ์ œ 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 ๋ฉ”์„œ๋“œ์— ๋ฆฌ์ŠคํŠธ๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ๊ฒฐ๊ณผ ์ถœ๋ ฅ.


 

 

+ Recent posts