요소 제거하기
Coddy Java 여정의 논리와 흐름 섹션에 포함된 레슨. 59개 중 24번째.
remove(element) 메서드는 지정된 요소가 HashSet에 있으면 제거합니다.
HashSet<String> fruits = new HashSet<>();
fruits.add("Apple");집합에서 "Apple"을 제거합니다
fruits.remove("Apple");위 코드를 실행한 후, 집합 fruits에는 다음이 포함됩니다:
[]챌린지
쉬움두 개의 인수를 받는 removeElement라는 메서드를 만드세요.
- 문자열의 HashSet (
set) - 제거할 문자열 (
element)
메서드는 지정된 요소를 집합에서 제거한 다음 업데이트된 집합을 출력해야 합니다.
직접 해보기
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);
}
}이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
논리와 흐름의 모든 레슨
직접 연습해 보세요: 온라인 Java 컴파일러