import java.util.HashSet; import java.util.Set; public class CollectionHasSet { public static void main(String[] args) { Set <String> hset = new HashSet< > (); hset.add("西瓜"); String[] fruit = {"鳳梨","草莓","西瓜" }; for(String p : fruit) hset.add(p); System.out.println(hset); } }執行結果
西瓜有加入嗎? 若改為 hset.add("葡萄"); 呢?
import java.util.HashSet; import java.util.Set; public class CollectionHasSet { public static void main(String[] args) { Set <String> hset = new HashSet< > (); hset.add("葡萄"); String[] fruit = {"鳳梨","草莓","西瓜" }; for(String p : fruit) hset.add(p); System.out.println(hset); } }執行結果
沒有留言:
張貼留言