集合和数组的区别:
Javase 提供了满足各种需求的API,在使用这些API之前,先了解其继承与接口操作结构,才能了解何时采用哪个类,以及类之间如何彼此合作,从而达到灵活应用。
集合按照存储结构可以分为两大类,分别是单列集合java.util.Collection 和双列结合java.util.Map.
Collection 是所有单列集合的父接口,因此在Collection中定义了单列集合通用的一些方法,这些方法可用于操作所有的单列集合。方法如下:
public class DemoCollection01 {
public static void main(String[] args) {
Collection<String> coll =new ArrayList<>();
coll.add("张三");
coll.add("李四");
coll.add("王五");
System.out.println(coll);
// 判断张三是否在集合中,用contains方法
System.out.println("判断张三是否在集合中"+coll.contains("张三"));
// 删除李四,并显示删除后的数据,用remove方法
System.out.println("删除李四"+coll.remove("李四"));
System.out.println("显示删除后的集合"+coll);
// size 集合中元素的个数
System.out.println("集合中共有"+coll.size()+"个元素");
// object[] toarray() 转换成一个数组
Object[] objects =coll.toArray();
// 对整个数组进行遍历
for (int i = 0; i < objects.length; i++) {
System.out.println(objects[i]);
}
// 清空集合
coll.clear();
System.out.println("集合的内容为"+coll);
System.out.println("集合是否为空:"+coll.isEmpty());
}
}
在程序开发中,经常需要遍历集合中所有的元素。JDK提供了一个接口java.util.Iterator. 该接口时Java集合中的一员,但它与collection、Map接口有所不同,Collection接口与Map接口主要用于储存元素,而Iteator 主要用于迭代访问Collection中的元素,因此Iterator对象也被称为迭代器。
想要遍历Collection集合,那么就要获取该集合迭代器完成迭代操作
迭代的概念:
public static void main(String[] args) {
Collection coll =new ArrayList<String>();
coll.add("串串星");
coll.add("牛郎星");
coll.add("汪星人");
Iterator<String> it=coll.iterator();
while(it.hasNext()){
String s=it.next();
System.out.println(s);
}
}
tips : 在进行集合元素取出时,如果集合中已经没有元素了,还继续使用迭代器的next() 方法,将会发生异常。
? 当遍历集合时,首先通过调用t集合itarator() 方法获迭代器对象,然后使用hasnext() 方法判断集合中是否存在下一个元素,如果存在,则调用next()方法将元素取出。
? Iterator迭代器对象在遍历集合时,内部采用指针的方式来跟踪集合中的元素
在调用iterator的next方法之前,迭代器的索引位于第一个元素之前,不指向任何元素,当第一次调用迭代器的next方法后,迭代器的索引会向后移动一位,指向第一个元素并将该元素返回,当再次调用next方法时,迭代器的索引会指向第二个元素并将该元素返回,一次类推,直到hasnext方法返回false,表示到达了集合的末尾,终止对元素的遍历。
tips: 一般在创建对象时,将未知的类型确定具体的类型,当没有指定泛型时,默认类型为Object类型。
public static void main(String[] args) {
Collection<String> list =new ArrayList<String>();
list.add("abc");
list.add("itcast");
// 当集合明确类型后,存放类型不一致就会编译报错
// 集合已经明确具体存放的元素类型,那么在使用迭代器的时候,迭代器也同样会知道具体遍历元素类型
Iterator<String> it =list.iterator();
// 当使用Iterator<string>控制元素类型后,就不需要强转了,获取大的元素就是string类型。
while (it.hasNext()){
String str =it.next();
System.out.println(str.length());
}
}
我们在集合中会大量的使用泛型,泛型用来灵活地将数据类型应用到不同的类、方法、接口当中。将数据类型作为参数进行传递。
定义格式:
修饰符 class 类名<代表泛型的变量> { }
classs ArrayList<E> {
public boolean add(E e)
}
使用泛型 : 即什么时候确定泛型。即在创建对象的时候确定泛型
例如 ArrayList<string> list = new ArrayList<String>();
public static void main(String[] args) {
// 创建一个泛型为String 的类
MyGenericClass<String> my =new MyGenericClass<String>();
// 调用setmvp方法
my.setMvp("大胡子等等");
String mvp=my.getMvp();
System.out.println(mvp);
// 创建一个泛型为Integer的类
MyGenericClass<Integer> my2 =new MyGenericClass<Integer>();
my2.setMvp(1233);
Integer mvp2 =my2.getMvp();
System.out.println(mvp2);
}
当使用泛型类或者接口时,传递的数据中泛型类型不确定,可以通过通配符<?> 表示。但是一旦使用泛型的通配符后,只能使用Object类中的共性方法,集合中元素自身方法无法使用。
public class Poker {
public static void main(String[] args) {
// 洗牌阶段
// 创建牌盒,用来存储牌面
ArrayList<String> pokerBox =new ArrayList<String>();
// 创建花色集合
ArrayList<String> colors =new ArrayList<String>();
// 创建数字集合
ArrayList<String> numbers =new ArrayList<String>();
// 将花色添加到 花色集合中国
colors.add("@");
colors.add("#");
colors.add("$");
colors.add("*");
// 将数字添加到数字集合中
for (int i = 1; i <=10 ; i++) {
numbers.add(i+" ");
}
numbers.add("gou");
numbers.add("quan");
numbers.add("k");
// 通过for循环将 花色和数字结合起来添加到牌面里
for(String color: colors){
for (String number:numbers){
String card =color + number;
pokerBox.add(card);
}
}
pokerBox.add("大王");
pokerBox.add("小王");
// System.out.println(pokerBox);
// 洗牌
Collections.shuffle(pokerBox);
// 发牌
ArrayList<String> player1 =new ArrayList<String>();
ArrayList<String> player2 =new ArrayList<String>();
ArrayList<String> player3 =new ArrayList<String>();
ArrayList<String> dipai =new ArrayList<String>();
// 遍历牌盒
for (int i = 0; i < pokerBox.size(); i++) {
String card =pokerBox.get(i);
if (i>=51){
dipai.add(card);
}else {
if (i % 3 == 0) {
player1.add(card);
} else if (i % 3 == 1) {
player2.add(card);
}else if (i%3==2){
player3.add(card);
}
}
}
System.out.println("玩家1"+player1);
System.out.println("玩家2"+player2);
System.out.println("玩家3"+player3);
System.out.println("底牌"+dipai);
}
原文:https://www.cnblogs.com/yuanjian-work/p/13762553.html