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();
- }
- }
来源: https://www.cnblogs.com/haizai/p/11071929.html