泛型

package onlinejudge;

import java.util.*;
class Course{
	public String id;
	public String name;
	public Course(String id, String name){
		this.id = id;
		this.name = name;
	}
}
/*泛型集合不可以使用基本数据类型,例如int , long , boolean等,如果非要使用,可以使用
*其包装类,如Integer,Long,Boolean
*/
//泛型集合可以添加泛型子类型元素
 class Test{
	 //只能存放Course元素的泛型List,名称为course
	List<Course>course;
	Test(){
		this.course = new ArrayList<Course>();
	}
	void testAdd() {
		Course cr1 = new Course("1","大学语文");
		course.add(cr1);
		Course cr2 = new Course("2","Java基础");
		course.add(cr2);
	}
	void ForEach() {
		for(Course c : course) {//不需要写成Object
			System.out.println(c.id + " " + c.name);
		}
	}
}
public class TestGeneric {
     public static void main(String []args) {
    	 Test t = new Test();
    	 t.testAdd();
    	 t.ForEach();
    	 List<Integer> list = new ArrayList<Integer>();
    	 list.add(1);
    	 System.out.println(list.get(0));
     }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值