Menu
Coddy logo textTech

요소 제거하기

Coddy Java 여정의 논리와 흐름 섹션에 포함된 레슨 — 59개 중 24번째.

remove(element) 메서드는 해당 요소가 HashSet에 존재하는 경우 지정된 요소를 제거합니다.

HashSet<String> fruits = new HashSet<>();
fruits.add("Apple");

세트에서 "Apple" 제거

fruits.remove("Apple");

위의 코드를 실행한 후, fruits 세트에는 다음이 포함됩니다:

[]
challenge icon

챌린지

쉬움

<strong>removeElement</strong>라는 이름의 메서드를 생성하세요. 이 메서드는 두 개의 인수를 받습니다:

  1. String의 HashSet (set)
  2. 제거할 String (element)
    이 메서드는 지정된 요소를 set에서 제거한 후 업데이트된 set을 출력해야 합니다.

치트 시트

remove(element) 메서드는 해당 요소가 존재하는 경우 HashSet에서 지정된 요소를 제거합니다:

HashSet<String> fruits = new HashSet<>();
fruits.add("Apple");
fruits.remove("Apple");

직접 해보기

import java.util.HashSet;
import java.util.Scanner;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;

public class Main {
    public static void removeElement(HashSet<String> set, String element) {
        // 여기에 코드를 작성하세요
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String setString = scanner.nextLine();
        String element = scanner.nextLine();
        
        Type setType = new TypeToken<HashSet<String>>(){}.getType();
        HashSet<String> set = new Gson().fromJson(setString, setType);
        
        removeElement(set, element);
    }
}
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

논리와 흐름의 모든 레슨