集合IV

 

Collections工具类:

 

 

(1)Colletion和Collections的区别?

 A:Collection是单列集合的顶层接口,定义了单列集合的共性功能。

 B:Collections是针对集合进行操作的工具类。

 

(2)Collections的功能:

A:排序

B:二分查找

C:最大值

D:反转

E:随机置换

 

(3)案例:

模拟洗牌

模拟洗牌,并对牌进行排序(HashMap,ArrayList,TreeSet)

代码体现:

import java.util.Collections;

import java.util.HashMap;

import java.util.TreeSet;

 

public class PokerTest {

         public static void main(String[] args) {

                    // 创建HashMap集合,键存储编号,值存储牌

                    HashMap<Integer, String> hm = new HashMap<Integer, String>();

                   // 创建ArrayList集合,存储编号

                   ArrayList<Integer> array = new ArrayList<Integer>();

 

                  // 定义花色数组

                 String[] colors = { "♠", "♥", "♣", "♦" };

                 // 定义点数数组

                 String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q","K", "A", "2" };

 

                             int index = 1;

                 for (String number : numbers) {

                          for (String color : colors) {

                                 array.add(index);

                                 hm.put(index++, color.concat(number));

                          }

                }

                           array.add(index);

                            hm.put(index++, "小王");

                            array.add(index);

                           hm.put(index, "大王");

 

                          // 洗牌

                        Collections.shuffle(array);

 

 

                     // 发牌

                    TreeSet<Integer> liuBoWen = new TreeSet<Integer>();

                    TreeSet<Integer> zhuGeLiang = new TreeSet<Integer>();

                    TreeSet<Integer> heShen = new TreeSet<Integer>();

                    TreeSet<Integer> diPai = new TreeSet<Integer>();

 

                     for (int x = 0; x < array.size(); x++) {

                              if (x >= array.size() - 3) {

                                        diPai.add(array.get(x));

                              } else if (x % 3 == 0) {

                                        liuBoWen.add(array.get(x));

                              } else if (x % 3 == 1) {

                                      zhuGeLiang.add(array.get(x));

                               } else if (x % 3 == 2) {

                                          heShen.add(array.get(x));

                              }

                     }

 

                   // 看牌

                  System.out.print("曹操:");

                 for (Integer key : liuBoWen) {

                              String value = hm.get(key);

                                        System.out.print(value + " ");

                  }

                                          System.out.println();

 

                    System.out.print(刘备:");

                     for (Integer key : zhuGeLiang) {

                            String value = hm.get(key);

                                          System.out.print(value + " ");

                    }

                                           System.out.println();

 

                 System.out.print("孙权:");

                 for (Integer key : heShen) {

                      String value = hm.get(key);

                                 System.out.print(value + " ");

                  }

                                  System.out.println();

 

                 System.out.print("底牌:");

                 for (Integer key : diPai) {

                           String value = hm.get(key);

                                    System.out.print(value + " ");

                }

                                             System.out.println();

           }

}

 

递归:

(1)方法定义中调用方法本身的现象。

 举例:老和尚给小和尚讲故事,我们学编程。

(2)递归的注意事项:

     A:递归一定要有出口,否则就是死递归。

     B:递归次数不能太多,否则内存溢出。

     C:构造方法不能递归调用。

(3)案例:

A:递归求阶乘:

代码体现:

/*

 * 递归求阶乘。

 * 

 * 求5的阶乘:

 *  n! = n*(n-1)!;

 *  n! = n*(n-1)*(n-2)*...*2*1;

 */

public class JieChengDemo {

          public static void main(String[] args) {

                   // 用循环做

                  int s = 1;

                 for (int x = 2; x <= 5; x++) {

                            s *= x;

                  }

                        System.out.println(s);

 

                // 用递归做  

                       System.out.println(jc(5));

              }

 

             

/*

 * 递归做:要定义一个功能。 假设我有一个功能jc(n)表示求n的阶乘。 那么,题:*n-1的阶乘怎么表示? jc(n-1)

 * 出口:n==1,返回值是1 规律:n!= n*(n-1)!

*/

              public static int jc(int n) {

                          if (n == 1) {

                              return 1;

                         } else {

                            return n * jc(n - 1);

                          }

               }

 

}

 

 

 

B:递归求兔子问题

代码体现:

/*

 

 * 有一对兔子,从出生后第3

*个月起每个月都生一对兔子,

 

*

* 小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,

 * 问每个月的兔子对数为多少? 

 * 第二十个月?

 * 

 *  兔子对数

 * 第一月: 1

 * 第二月: 1

 * 第三月: 2

 * 第四月: 3

 * 第五月: 5

 * 第六月: 8

 * ...

 * 

 * 看规律:(斐波那契数列)

 *  1,1,2,3,5,8...

 * 

 * 规律:

 *  规律:从第3项开始,每一项是前两项之和。

 *  出口:第一项和第二项是已知道的。

 */

public class RabbitDemo {

            public static void main(String[] args) {

                         // 用递归实现

                          System.out.println(fun(20));

 

                        // 用数组实现

                       int[] arr = new int[20];

                              arr[0] = 1;

                              arr[1] = 1;

                        for (int x = 2; x < arr.length; x++) {

                                    arr[x] = arr[x - 1] + arr[x - 2];

                        }

                               System.out.println(arr[19]);

 

                               int a = 1;

                               int b = 1;

                               for (int x = 0; x < 18; x++) {

                                     int temp = a;

                                      a = b;

                                      b = temp + b;

                               }

                                    System.out.println(b);

 

                 }

 

/*

 * 假设我有一个功能fun(n)表示第n项。 请问,第n-1项和第n-2项怎么表示: *fun(n-1),fun(n-2)

*/

              public static int fun(int n) {

 

 

                        if (n == 1 || n == 2) {

                               return 1;

                         } else {

                             return fun(n - 1) + fun(n - 2);

                         }

               }

}

 

 

 

C:递归获取指定目录下所有java文件的绝对路径:import java.io.File;

代码体现:

/*

 * 需求:获取D:\itheima\20140623下所有java文件的绝对路径。并输出。

 * 

 * 分析:

 *  A:封装目录

 *  B:获取该目录下所有文件或者文件夹File数组。

 *  C:遍历File数组,获取每一个File对象。

 *  D:判断该File:

 *  文件夹:回到B。

 *  文件:判断是否java结尾

 *  如果是,就输出绝对路径。

 */

public class FilePathDemo {

            public static void main(String[] args) {

                      // 封装目录

                     File srcFile = new File("d:\\itcast\\20140326");

 

                      // 遍历功能

                   getAllFilePath(srcFile);

               }

 

          public static void getAllFilePath(File srcFile) {

                  // 获取该目录下所有文件或者文件夹File数组。

                  File[] fileArray = srcFile.listFiles();

 

                   // 遍历File数组,获取每一个File对象。

                  for (File file : fileArray) {

                           if (file.isDirectory()) {

                                // 文件夹:回到B。

                               getAllFilePath(file);

                           } else {

                               // 判断是否java结尾

                                if (file.getName().endsWith(".java")) {

                                    // 就输出绝对路径。

                                   System.out.println(file.getAbsolutePath());

                                 }

                          }

                  }

            }

}

 

 

 

 

D:递归删除带内容的目录:

代码体现:

import java.io.File;

 

/*

 * 删除一个带内容的文件夹。

 * 

 * 分析:

 *  A:封装目录

 *  B:获取该目录下所有文件或者文件夹File数组。

 *  C:遍历File数组,获取每一个File对象。

 *  D:判断该File:

 *  文件夹:回到B。

 *  文件:就删除。

 */

public class DeleteFileDemo {

           public static void main(String[] args) {

          // 封装目录

         File srcFile = new File("test");

 

        // 递归删除

        deleteFiles(srcFile);

       }

 

       private static void deleteFiles(File srcFile) {

                   // 获取该目录下所有文件或者文件夹File数组。

                   File[] fileArray = srcFile.listFiles(); // 可能返回null

 

                   // 加入不为null的判断,就可以删除你想删除的任意目录

                   if (fileArray != null) {

                 // 遍历File数组,获取每一个File对象。

                   for (File file : fileArray) {

                          // 判断该File:

                         if (file.isDirectory()) {

                               deleteFiles(file);

                          } else {

                               System.out.println(file.getName() + "---" + file.delete());

                           }

                    }

 

                                 // 删除文件夹

                                  System.out.println(srcFile.getName() + "---" + srcFile.delete());

                       }

         }

}

 

 

 

 

 

 详细请查看:http://edu.youkuaiyun.com

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值