import java.io.*; import java.util.*; public class Test { public static void main(String args[]) throws IOException { int i1, sw; System.out.println("HashSetオブジェクト a を生成"); HashSet <String> a = new HashSet <String> (); a.add("mno"); System.out.println(" mno を追加"); a.add("JKL"); System.out.println(" JKL を追加"); a.add("ghi"); System.out.println(" ghi を追加"); a.add("DEF"); System.out.println(" DEF を追加"); a.add("abc"); System.out.println(" abc を追加"); System.out.print(" 結果は"); Iterator it = a.iterator(); while (it.hasNext()) System.out.print(" " + it.next()); System.out.println(); System.out.println("要素の削除"); System.out.print(" 要素 \"abc\" の削除"); a.remove("abc"); it = a.iterator(); while (it.hasNext()) System.out.print(" " + it.next()); System.out.println(); System.out.print(" 一つおきに削除"); it = a.iterator(); sw = 1; while (it.hasNext()) { it.next(); if (sw > 0) it.remove(); sw *= (-1); } it = a.iterator(); while (it.hasNext()) System.out.print(" " + it.next()); System.out.println(); } }
HashSetオブジェクト a を生成 mno を追加 JKL を追加 ghi を追加 DEF を追加 abc を追加 結果は abc DEF ghi JKL mno 要素の削除 要素 "abc" の削除 DEF ghi JKL mno 一つおきに削除 ghi mno