新手小白一枚,请教一个关于Generic的问题

Humphrey_Huang 2014-07-11 08:31:58



JDK版本是1.7,Eclipse里面也设置好了。

直接上图,请问里应该怎么处理呢?
谢谢大家了。
...全文
299 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
于金龙 2014-07-21
  • 打赏
  • 举报
回复
为什么我执行你那段代码
Collection<String> c2=new HashSet<String>();
		c2.add("aaa");
		c2.add("bbb");
		c2.add("ccc");
		for(Iterator<String> it=c2.iterator();it.hasNext();){
			String s=it.next();
			System.out.println(s);
		}
就可以执行啊? 难道1.6跟1.7差距就这么大?
日知己所无 2014-07-20
  • 打赏
  • 举报
回复
引用 3 楼 Humphrey_Huang 的回复:
Got it , tks!
客页! 别忘了结贴,额滴分啊!……
Humphrey_Huang 2014-07-11
  • 打赏
  • 举报
回复
Got it , tks!
guest6379 2014-07-11
  • 打赏
  • 举报
回复
你确定设置好了jre7?

       Collection<String> c = new HashSet<>();
        c.add("a");
        c.add("b");
        
        Iterator<String> it = c.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
日知己所无 2014-07-11
  • 打赏
  • 举报
回复

import java.util.*;

public class BasicGeneric {

    public static void main(final String[] args) {
        List<String> list = new ArrayList<>(); // 这一句最好这么写
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");

        // 更好的写法
        for (String element : list) {
            System.out.println(element);
        }

        // Java8新特性:Lambda 表达式
        list.stream().forEach((element) -> {
            System.out.println(element);
        });

        Collection<String> collection = new HashSet<>(); // 这一句最好这么写
        collection.add("111");
        collection.add("222");
        collection.add("333");

        // OK的版本1
        for (Iterator<String> iterator = collection.iterator(); iterator.hasNext();) {
            String element = iterator.next();
            System.out.println(element);
        }

        // OK的版本2
        for (Iterator iterator = collection.iterator(); iterator.hasNext();) {
            String element = iterator.next().toString();
            System.out.println(element);
        }
    }
}
aaa
bbb
ccc
aaa
bbb
ccc
111
222
333
111
222
333
原来的代码貌似没啥问题,试试【OK的版本2】的写法如何?

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧