是集合的最顶层的接口,定义了集合共性的方法
接口无法直接创建对象,使用多态的方式创建对象
collection<集合中的数据类型(泛型)> coll = new arraylist<集合中的数据类型(泛型)>();
迭代器
集合中存储数据的方式(数据类型)不一样,取出集合中元素的方式也不同,java给我们提供了一种公共的取出元素的方式,叫迭代器
描述迭代器的接口:java.util.iterator
接口中的抽象方法:
boolean hasnext() 如果仍有元素可以迭代,则返回 true。 判断集合中还有没有元素,有返回true,没有返回false
e next() 返回迭代的下一个元素。 取出集合中的下一个元素
迭代器是一个接口,需要找到迭代器的实现类,迭代器的实现类是每个集合的内部类
在collection接口中有一个方法: iterator方法返回的就是迭代器
iterator<e> iterator() 返回在此 collection 的元素上进行迭代的迭代器。
arraylist集合实现了collection接口,重写iterator方法,方法的返回值就是迭代器的实现类对象
注意:我们只需要知道iterator方法返回的而是迭代器的实现类就行了,不需要关注返回的是哪个实现类对象,这种变成方式叫做面向接口编程
迭代器的使用步骤:
1.创建集合对象,往集合中添加元素
2.使用集合中的方法iterator获取迭代器的实现类对象,使用iterator接口接收(多态)
3.使用iterator中的方法hasnext和next方法进行迭代,取出集合中的元素
1 public static void main(string[] args) { 2 //1.创建集合对象,往集合中添加元素 3 //collection<string> coll = new arraylist<string>(); 4 collection<string> coll = new hashset<string>(); 5 coll.add(姚明); 6 coll.add(乔丹); 7 coll.add(詹姆斯); 8 coll.add(科比); 9 coll.add(艾弗森);10 //2.使用集合中的方法iterator获取迭代器的实现类对象,使用iterator接口接收(多态)11 //集合中的数据类型是什么,迭代器的数据类型就是什么,跟着集合走12 iterator<string> it = coll.iterator();13 //3.使用iterator中的方法hasnext和next方法进行迭代,取出集合中的元素14 //boolean hasnext() 如果仍有元素可以迭代,则返回 true。15 /*boolean b = it.hasnext();16 system.out.println(b);17 //e(string) next() 返回迭代的下一个元素。18 string s = it.next();19 system.out.println(s);20 21 b = it.hasnext();22 system.out.println(b);23 s = it.next();24 system.out.println(s);25 26 b = it.hasnext();27 system.out.println(b);28 s = it.next();29 system.out.println(s);30 31 b = it.hasnext();32 system.out.println(b);33 s = it.next();34 system.out.println(s);35 36 b = it.hasnext();37 system.out.println(b);38 s = it.next();39 system.out.println(s);40 41 b = it.hasnext();42 system.out.println(b);//false,没有元素了43 s = it.next();//没有元素了,在取就报nosuchelementexception没有元素异常44 system.out.println(s);*/45 46 /*47 * 发现以上迭代的过程是一个重复的过程,可以使用循环优化48 * 我们不知道集合中有多少元素,所以可以使用while循环49 * while循环的结束条件:hasnext();返回false50 */51 while(it.hasnext()){52 string s = it.next();53 system.out.println(s);54 }55 system.out.println(-------------------);56 /*57 * for循环方式迭代器,使用不多58 */59 /*for(iterator<string> it2 = coll.iterator();it2.hasnext();){60 string s = it2.next();//取出元素,移动指针到下一位61 system.out.println(s);62 }*/63 }
并发修改异常
在迭代的过程中,对集合的长度进行了修改,就会发生并发修改异常
遍历的过程中,集合的长度进行了修改,但是迭代器并不知道,就会产生concurrentmodificationexception
解决方法:
1.迭代就是迭代,不要对集合进行修改
2.使用迭代器iterator的子接口listiterator中的方法add/remove,让迭代器自己增加往集合中增加元素/移除元素
这样迭代器本身知道了集合的变化,就不会产生并发修改异常了
void add(e e) 将指定的元素插入列表(可选操作)。
void remove() 从列表中移除由 next 或 previous 返回的最后一个元素(可选操作)。
1 public static void main(string[] args) { 2 arraylist<string> list = new arraylist<string>(); 3 4 list.add(null); 5 list.add(abc1); 6 list.add(abc2); 7 list.add(abc3); 8 list.add(abc4); 9 10 /*11 * 使用迭代器遍历集合12 */13 //获取迭代器14 iterator<string> it = list.iterator();15 //使用while遍历集合16 while(it.hasnext()){17 string s = it.next();18 19 /*20 * 判断集合中有没有abc3这个元素21 * 如果有,增加一个元素itcast22 * 编程技巧:使用equals判断的时候,要把已知的变量写在前边,未知的写在后边,防止空指针异常23 */24 //if(s.equals(abc3)){25 if(abc3.equals(s)){26 //1.迭代就是迭代,不要对集合进行修改27 //list.add(itcast);28 }29 30 system.out.println(s);31 }32 33 system.out.println(------------------);34 35 /*36 * 2.使用迭代器iterator的子接口listiterator中的方法add/remove,让迭代器自己增加往集合中增加元素/移除元素37 */38 listiterator<string> listit = list.listiterator();39 while(listit.hasnext()){40 string s = listit.next();41 if(abc3.equals(s)){42 listit.add(itcast);43 }44 system.out.println(s);45 }46 system.out.println(list);47 }
增强for
内部是一个迭代器,简化了迭代的代码,使遍历更加简单
collection接口继承iterable,所以collection接口的所有实现类都可以是用增强for
注意:增强for是jdk1.5之后出现的
格式:
for(数据类型(集合/数组的数据类型) 变量名 : 集合名/数组名){
syso(变量名);
}
java中的泛型
就是数据类型,在创建对象的时候确定
java中的泛型是一个伪泛型:在编译的时候有(写代码.java中),运行的时候(.class)没有
随机数:伪随机数
泛型的好处:
1.避免强转,可以直接使用元素特有的方法
2.把运行期的异常,转换编译期异常(编译失败)
定义含有泛型的类
模仿arraylist集合
public class arraylist<e>{}
e:是一个未知的数据类型,可能是integer,可能是string,可能person
创建类对象的时候确定数据类型
定义格式:
修饰符 class 类名<e>{
}
1 public class genericclass<e> { 2 private e name; 3 4 public e getname() { 5 return name; 6 } 7 8 public void setname(e name) { 9 this.name = name;10 }11 12 public void method(e e){13 system.out.println(e);14 }
定义含有泛型的接口
格式:
修饰符 interface 接口名<泛型>{
抽象方法(参数<泛型>);
}
1 public interface genericinterface<e> {2 public abstract void method(e e);3 }
1 /* 2 * 1.定义接口的实现类,不管泛型,接口泛型是怎么写的,实现类也怎么写 3 * public class arraylist<e> implements list<e>{} 4 * 创建实现类对象的时候确定泛型的数据类型 5 */ 6 class genericinterfaceimpl1<e> implements genericinterface<e>{ 7 8 @override 9 public void method(e e) {10 system.out.println(e);11 }12 }
含有泛型的方法
不是类上定义的泛型,是方法自己定义的泛型
定义格式:在修饰符和返回值类型之间要定义泛型,才能使用
修饰符 <泛型> 返回值类型 方法名(参数<泛型>){
}
方法上的泛型,在调用方法的时候确定数据类型,传递的是什么类型的数据,泛型就是什么类型(和类上的泛型没有关系)
1 public class genericmethod<e> { 2 3 /* 4 * 定义方法,使用类上的泛型 5 */ 6 public void method(e e){ 7 system.out.println(e); 8 } 9 10 /*11 * 定义一个含有泛型的方法12 */13 public <t> void function(t t){14 system.out.println(t);15 }
泛型的通配符:?,代表任意的数据类型
上限限定:? extends e代表只要是e类型的子类即可
下限限定:? super e代表只要是e类型的父类即可
arraylist集合的构造方法
arraylist(collection<? extends e> c)
参数是一个集合,集合的数据类型有要求,只能是arraylist泛型的子类或者是本身
arraylist(collection<? extends e> c)
参数是一个集合,集合的数据类型有要求,只能是arraylist泛型的子类或者是本身
1 /* 2 * 斗地主案例: 3 * 1.准备牌 4 * 2.洗牌 5 * 3.发牌 6 * 4.看牌 7 */ 8 public class doudizhu { 9 public static void main(string[] args) {10 //1.准备牌11 //创建存储54张牌的集合12 arraylist<string> poker = new arraylist<string>();13 //存储大王小王14 poker.add(大王);15 poker.add(小王);16 //存储52张牌17 //创建序号的数组18 string[] numbers = {2,a,k,q,j,10,9,8,7,6,5,4,3};19 //创建花色数组20 string[] colors = {?,?,?,?};21 //嵌套遍历两个数组22 for (string number : numbers) {23 for (string color : colors) {24 //system.out.println(color+number);25 //把组合的牌放入到集合中26 poker.add(color+number);27 }28 }29 //system.out.println(poker);30 31 /*32 * 2.洗牌33 * 使用collections中的方法34 * static void shuffle(list<?> list) 35 */36 collections.shuffle(poker);37 //system.out.println(poker);38 39 /*40 * 3.发牌41 * 创建4个集合42 * 遍历poker集合43 * 使用poker集合的索引%3发牌44 */45 arraylist<string> player01 = new arraylist<string>();46 arraylist<string> player02 = new arraylist<string>();47 arraylist<string> player03 = new arraylist<string>();48 arraylist<string> dipai = new arraylist<string>();49 //遍历poker集合50 for (int i = 0; i < poker.size(); i++) {51 //获取牌52 string s = poker.get(i);53 //先判断索引是否为底牌的索引 51 52 5354 if(i >=51){55 //给底牌发牌56 dipai.add(s);57 }else if(i%3==0){58 //给玩家1发牌59 player01.add(s);60 }else if(i%3==1){61 //给玩家1发牌62 player02.add(s);63 }else if(i%3==2){64 //给玩家1发牌65 player03.add(s);66 }67 }68 //4.看牌69 system.out.println(刘德华:+player01);70 system.out.println(周润发:+player02);71 system.out.println(周星驰:+player03);72 system.out.println(底牌:+dipai);73 }74 }
以上就是迭代器、增强for、泛型用法集合的详细内容。
