码迷,mamicode.com
首页 > 其他好文 > 详细

Generics and Collection (1)

时间:2015-05-02 13:44:56      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:

 1 public static void main(String args[]) {
 2         List ints = Arrays.asList(new Integer[]{new Integer(1), new Integer(3)});
 3         int s =0;
 4         for(Iterator it = ints.iterator(); it.hasNext();)
 5         {
 6             int n = ((Integer)it.next()).intValue();
 7             s+=n;
 8         }
 9         System.out.println(s);
10     }

 

public static void main(String args[]) {
        List<Integer> ints = Arrays.asList(1,2,3);
        int s = 0;
        for(int n: ints)
        {
            s+=n;
        }
        System.out.println(s);
    }

 

public static void main(String args[]) {
        List<String> words = new ArrayList<String>();
        words.add("Hello");
        words.add("world");
        String s = words.get(0) +words.get(1);        
        System.out.println(s);
    }

 

package cn.galc.test;

import java.util.*;
 
class Lists{
    public static <T> List<T> toList(T[] arr)
    {
        List<T> list = new ArrayList<T>();
        for (T t: arr)
        {
            list.add(t);
        }
        return list;
    }
}
 
public class Test {     
    public static void main(String args[]) {
        List<String> words = Lists.toList(new String[]{"sun","ming"});
        for(String s:words)
            System.out.println(s);
    }
}

 

Generics and Collection (1)

标签:

原文地址:http://www.cnblogs.com/Ring1981/p/4471808.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!