set中只有一条元素,怎么取啊,不会要也写个for吧

解决方案 »

  1.   

    Set<Integer> ints = new HashSet<Integer>();
    a = ints.iterator().next();
    不过你最好确定ints中至少有1个元素。
      

  2.   

    Set<Integer> ints = new HashSet<Integer>();
    Iterator<Integer> it = ints.iterator();
    if(it.hasNext()){
        a = it.next();
    }如果有多个元素,就把if改成while。
      

  3.   

    木有别的办法了?想list里面get(0)那样
      

  4.   

    new ArrayList<Integer>(set).get(0);
      

  5.   

    set要用迭代器的,和list一样,都要先判断是否为空,是否有对象
      

  6.   

    用for,Iterator都没关系啊,只有一个就计算一次,就算用List那种,时间复杂度都一样的