当前位置: 移动技术网 > IT编程>软件设计>面向对象 > java基础第十二篇之集合、增强for循环、迭代器和泛型

java基础第十二篇之集合、增强for循环、迭代器和泛型

2019年06月23日  | 移动技术网IT编程  | 我要评论

collection接口中的常用方法:
* 所有的子类子接口都是具有的
* 集合的方法:增删改查
*
* public boolean add(e e);//添加元素 返回值表示是否添加成功
* public boolean remove(object o);//删除元素,返回值表示是否删除成功
* 没有修改方法
* 查询元素的方法没有,但是
* public int size();//查询集合集合中元素的个数
* //其他方法:
* public void clear();//清空集合
* public object[] toarray();//把集合转成数组
* //判断方法
* public boolean contains(object o);//判断集合中是否包含指定元素

collection是接口,collection下有很多子类,
* 有的子类有下标,有的子类没有下标,不能通过下标去遍历
*
* collection就定义一个中 所有子类共用遍历集合的方式:迭代器方法
*
* 使用迭代器遍历collection集合的步骤:
*
* 1.定义集合对象(随便collection哪一个子类都可以)
*
* 2.通过集合对象.iterator()
*
* 3.调用 迭代器.hasnext() 迭代器.next()
*
* 4.反复执行3步骤 直到 迭代器告诉你false
*
* 以上方式使用于所有collection的实现类
*


* 增强for循环:有名foreach循环
* 格式:
* for(元素的数据类型 变量名:集合/数组){
* syso(变量名);
* }
*
*
* 增强for循环的底层 使用迭代器去实现
*
*
* 增强for循环什么时候使用?
* 当你仅仅需要遍历,查看数据的时候使用

public static void main(string[] args) {
// todo auto-generated method stub
//遍历数组
// int[] nums = {1,2,3,4,5};
// for(int num:nums){
// system.out.println(num);
// }
collection<integer> nums = new arraylist<integer>();
nums.add(10);
nums.add(20);
nums.add(30);
nums.add(40);
//1.用迭代器
//2.增强for循环
for(integer i:nums){
nums.add(50);
system.out.println(i);
}
//快捷键 foreach+alt+/

* 使用iterator对象循环遍历集合,
* 如果出现”baidu”字符串,那么向集合中添加一个大写的”baidu”字符串
*
* 出现了一个异常:
* concurrent(并发)modification(修改)exception:并发修改异常
* 当你使用迭代器遍历集合的时候,如果你在遍历的过程给集合添加或者删除元素
*
* 迭代器对象获取出来的时候,具有一个记忆功能,记录了集合中元素个数
* 在迭代的过程 如果你添加了 那么记忆长度和实际的长度不一样


* 泛型: 是一种不确定的类型
* 格式: <e>,<p>,<q> ,<k>,<v>
* 1.他是一种安全机制?(把运行时的问题转移到了编译时)
* 2.减少了我们代码量
* 3.避免了强制类型转换
*
* 我们在开发中会使用大量的java定义好的泛型
*
* 但是我们很少自己写泛型自己用

以arraylist<e>泛型,泛型中<e>的含义
* e是一个变量,等待接收一个引用数据类型
*
* <e>在java中的使用,可以用类上,接口上,方法上
*
* 1.泛型<e>用在类上,java的arraylist<e>
* 泛型类中的泛型什么时候确定?在创建泛型类对象的时候确定
*
* 2.泛型<e>用在方法上:
* 格式:public <t> 返回值类型 方法名(t t){....}
* 泛型方法中的泛型什么时候确定?在调用的时候确定,调用一次确定一次
* 3.泛型<e>用在接口上:java中collection<e>
* 泛型接口中的泛型什么时候确定?
* 3.1实现类实现接口的时候,确定接口中的泛型
* 3.2实现类不确定泛型,而把接口的泛型也继承过来,
* 这个实现类创建对象的时候确定

public class genericdemo02 {

public static void main(string[] args) {
// todo auto-generated method stub
//3.泛型接口
// myclass2<string> mc2 = new myclass2<string>();
// mc2.show("abc");
myclass2<integer> mc3 = new myclass2<integer>();
mc3.show(12);
}
//求两个整数的和,求两个float类的数的和,求两个double类型数的和
public static <t> t sum(t num1,t num2){
//return num1+num2;
return num1;
}

// public static float sum(float num1,float num2){
// return num1+num2;
// }

/*
* 泛型方法的使用
*/
public static void demo02(){
//创建一个person
person<string> p = new person<string>();
p.show(123);//传递123 那么这泛型t就是 integer
p.show("123");//传递"123",那么这个泛型t就是string类型
}
/*
* 泛型类的使用
*/
public static void demo01(){
//1.泛型在类上
//person p = new person();
// p.setname(123);//因为参数定死就是string类型
// person<string> p = new person<string>();
// p.setname("张三");
// string name = p.getname();
person<integer> p2 = new person<integer>();
p2.setname(123);
integer i = p2.getname();
}

}


public class person<e> {//称为这个类是泛型类
e name;

public e getname() {
return name;
}

public void setname(e name) {//使用的类的泛型的方法
this.name = name;
}

public <t> t show(t e){//泛型方法
//system.out.println(e);
return e;
}

}

*
* 泛型通配符: ? 任意类型
*
* 复合格式:
* ? extends string://表示这种类型要么是string 要么string的子类
* ? extends animal://
*
* ? super animal://表示这种类型 要么是animal 要么是animal的父类
* 在arraylist<e>中有一个成员方法:
* boolean addall(collection<? extends e> c)
*
*/

*
* 1.创建一个集合,存储54张牌
* 2.洗牌(把集合中的元素打乱顺序)
* 3.发牌(一人一张 轮流)
* 4.展示牌(3个人的牌,展示地主牌)
*
*
*/
public class doudizhudemo {
public static void main(string[] args) {
//1.创建一副牌,保存到集合中
arraylist<string> cards = new arraylist<string>();
//添加牌 一张牌 数值+花色
string[] nums = {"a","2","3","4","5","6","7","8","9","10","j","q","k"};
string[] colors = {"♠","♥","♣","♦"};
//拼接 花色+num
for (string num : nums) {
for (string color : colors) {
string card = color+num;
cards.add(card);
}
}
cards.add("大s");
cards.add("小s");
//2.洗牌 java提供了一个方法 collections.shuffle(集合);
collections.shuffle(cards);//打乱集合中元素的顺序
//3.发牌
//定义三个集合
arraylist<string> p1 = new arraylist<string>();
arraylist<string> p2 = new arraylist<string>();
arraylist<string> p3 = new arraylist<string>();
//定义地主牌的集合
arraylist<string> dp = new arraylist<string>();
//遍历54张牌
for (int i = 0; i < cards.size(); i++) {
string card = cards.get(i);
//如果是最后三张,不发,保存到地主牌的集合中
//i 53 52 51
if(i>=51){
//不发
dp.add(card);

}else{
//0 p1 1 p2 2 p3 3 p1
if(i%3==0){
//给p1发牌
p1.add(card);
}else if(i%3==1){
p2.add(card);
}else{
p3.add(card);
}
}
}
//4.展示牌(打印所有的牌)//
//调用方法
lookcard(p1);
lookcard(p2);
lookcard(p3);
lookcard(dp);

}
//看牌方法
public static void lookcard(arraylist<string> cards){
for (string card : cards) {
system.out.print(card+" ");
}
system.out.println();
}
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网