首页 > 其他 > 详细

[好程序员笔记分享]42 类集的框架二

时间:2015-03-26 19:56:28      阅读:252      评论:0      收藏:0      [点我收藏+]

android培训------我的java笔记,期待与您交流!

1、Collection和Iteration接口
技术分享
Iteration : hasNext()、next()

2、Set和HashSet的使用方法

import java.util.Set;
import java.util.HashSet;

public class Test{
 public static void main(String args[]){
  HashSet<String> hashSet = new HashSet<String>();
  Set<String> set = hashSet;//向上转型
  set.add("a");
  set.add("b");
  set.add("c");
  set.add("d");
  set.add("c");//重复
 
  int i = set.size();
  System.out.println(i);//长度4
 
  System.out.println(set.isEmpty());
 
  set.remove("a");
  i = set.size();
  System.out.println(i);//3
 
  set.clear();
  i = set.size();
  System.out.println(i);//clear后长度为0
  System.out.println(set.isEmpty());
    
 }
}
技术分享

import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;

public class Test{
 public static void main(String args[]){
  HashSet<String> hashSet = new HashSet<String>();
  Set<String> set = hashSet;//向上转型
  set.add("a");
  set.add("b");
  set.add("c");
  set.add("d");
  set.add("c");//重复
 
  //迭代器:iterator
  //方法:hasNext(),next()
  //Iterator<--Collection<--Set<--HashSet(子)
  //Iterator<--Collection<--List<--ArrayList(子)
 
  //调用Set对象的iterator()方法,会生成一个迭代器对//象,该对象用于遍历整个Set.
  Iterator<String> it = set.iterator();
 
  //boolean bl = it.hasNext();
  while(it.hasNext()){
   String s = it.next();//取值,指向下一个
   System.out.println(s);
  }
 
 }
}




[好程序员笔记分享]42 类集的框架二

原文:http://www.cnblogs.com/AJimmy09/p/4369238.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!