黑马程序员_专题:工具类的复习(Arrays工具类,Collections工具类,System系统类,Runtiem类)

------- android培训java培训、期待与您交流! ----------

------------------------Arrays工具类--------------------------------------------------------------------------------------

  1. /** Arrays:是用来操作数组的工具类。 
  2.  * 方法都是静态的,通过类名直接使用。 
  3.  *  
  4.  * 功能: 
  5.  *      public static String toString(int[] a):把数组转成字符串形成 
  6.  *      public static void sort(int[] a):对数组进行排序 
  7.  *      public static int binarySearch(int[] a, int key):二分查找法  
  8.  */  
  9. public class ArraysDemo {  
  10.     public static void main(String[] args) {  
  11.         // 定义一个数组  
  12.         int[] arr = { 4537948263 };  
  13.   
  14.         // 需求:写一个功能实现把数组转成字符串 格式:[元素1,元素2,元素3...]  
  15.         // String s = arrayToString(arr);  
  16.         String s = Arrays.toString(arr);  
  17.         System.out.println("s:" + s);  
  18.   
  19.         // 排序  
  20.         Arrays.sort(arr);  
  21.         System.out.println("arr:" + Arrays.toString(arr));  
  22.   
  23.         // 二分查找法  
  24.         int index = Arrays.binarySearch(arr, 82);  
  25.         System.out.println("index:" + index);  
  26.     }  
  27.   
  28.     public static String arrayToString(int[] arr) {  
  29.         StringBuilder sb = new StringBuilder();  
  30.         sb.append("[");  
  31.         // 遍历数组,追加到sb中。  
  32.         for (int x = 0; x < arr.length; x++) {  
  33.             if (x == arr.length - 1) {  
  34.                 sb.append(arr[x]);  
  35.             } else {  
  36.                 sb.append(arr[x]).append(",");  
  37.             }  
  38.         }  
  39.         sb.append("]");  
  40.         return sb.toString();  
  41.     }  
  42. }  


-------------------------------Collections工具类----------------------------------------------------------------------------

  1. /**Collections:是用来操作Collection集合的工具类。 
  2.  *  
  3.  * 面试题:Collection和Collections的区别? 
  4.  *        Collection是接口,定义了Collection集合的共性内容。 
  5.  *        Collections是操作Collection集合类的工具类。 
  6.  *  
  7.  * 要掌握的功能: 
  8.  *        public static int binarySearch(List list, T key) 
  9.  *        public static T max(Collection coll)  
  10.  *        public static void reverse(List list)  
  11.  *        public static void shuffle(List list)   
  12.  *        public static void sort(List list)  
  13.  *        public static List synchronizedList(List list)  
  14.  */  
  15. public class CollectionsDemo {  
  16.     public static void main(String[] args) {  
  17.         // 创建集合对象  
  18.         List<Integer> list = new ArrayList<Integer>();  
  19.         // 这样做完以后,list就是线程安全的。  
  20.         // List<Integer> list = Collections.synchronizedList(new  
  21.         // ArrayList<Integer>());  
  22.   
  23.         // 添加元素  
  24.         list.add(9);  
  25.         list.add(5);  
  26.         list.add(4);  
  27.         list.add(6);  
  28.         list.add(7);  
  29.         list.add(8);  
  30.         list.add(3);  
  31.   
  32.         // public static T max(Collection coll)  
  33.         // Integer i = Collections.max(list);  
  34.         // System.out.println("i:" + i);  
  35.   
  36.         // public static void reverse(List list)  
  37.         // Collections.reverse(list);  
  38.   
  39.         // public static void sort(List list)  
  40.         // Collections.sort(list);  
  41.   
  42.         // public static int binarySearch(List list, T key)  
  43.         // int index = Collections.binarySearch(list, 6);  
  44.         // System.out.println("index:" + index);  
  45.   
  46.         // public static void shuffle(List list)  
  47.         // 每次调用,随机把集合中的数据排序。洗牌  
  48.         Collections.shuffle(list);  
  49.   
  50.         System.out.println("list:" + list);  
  51.     }  
  52. }  


 

  1. /* 
  2.  * 模拟扑克牌的洗牌。 
  3.  *  
  4.  * 思路: 
  5.  *      1:创建一副新牌 
  6.  *          黑桃 A,2,3,4,...K  
  7.  *          红桃 A,2,3,4,...K 
  8.  *          梅花 A,2,3,4,...K 
  9.  *          方块 A,2,3,4,...K 
  10.  *      2:通过分析,我们发现每张牌用字符串类型接受。 
  11.  *      3:创建一个花色数组,创建一个牌大小数组。 
  12.  *      4:创建一个集合,把两个字符中的数据按照牌进行拼接,然后存入集合中。加入大小鬼。 
  13.  *      5:模拟发牌 
  14.  */  
  15. public class CardDemo {  
  16.     public static void main(String[] args) {  
  17.         // 创建一副新牌  
  18.         // 创建花色  
  19.         String[] colors = { "黑桃""红桃""梅花""方块" };  
  20.         // 创建牌  
  21.         String[] cards = { "A""2""3""4""5""6""7""8""9""10",  
  22.                 "J""Q""K" };  
  23.         // 创建集合  
  24.         ArrayList<String> array = new ArrayList<String>();  
  25.   
  26.         for (int x = 0; x < colors.length; x++) {  
  27.             for (int y = 0; y < cards.length; y++) {  
  28.                 // 创建牌,并加入集合  
  29.                 array.add(colors[x].concat(cards[y]));  
  30.             }  
  31.         }  
  32.         // 加入大小王  
  33.         array.add("大王");  
  34.         array.add("小王");  
  35.   
  36.         // 洗牌  
  37.         Collections.shuffle(array);  
  38.   
  39.         System.out.println(array);  
  40.     }  
  41. }  


-----------------------------------System系统类--------------------------------------------------------------------------------

  1. /*System:系统类,提供一些类字段和方法供我们使用。 
  2.  *  
  3.  * 要掌握的功能: 
  4.  *      public static void exit(int status):退出jvm。根据惯例,非0的状态码表示异常终止。 
  5.  *      public static Properties getProperties():获取系统的属性。 
  6.  *      public static long currentTimeMillis():获取当前时间的毫秒值。测试程序的运行时间。 
  7.  */  
  8. public class SysetmDemo {  
  9.     public static void main(String[] args) {  
  10.         // System.out.println("haha");  
  11.         // System.exit(0);  
  12.   
  13.         // Properties prop = System.getProperties();  
  14.         // Set<Object> set = prop.keySet();  
  15.         // for (Object key : set) {  
  16.         // Object value = prop.get(key);  
  17.         // System.out.println(key + "***" + value);  
  18.         // }  
  19.   
  20.         // 测试:  
  21.         long start = System.currentTimeMillis();  
  22.         // String s = "";  
  23.         // for (int x = 0; x < 100000; x++) {  
  24.         // s += x;  
  25.         // }  
  26.   
  27.         StringBuilder sb = new StringBuilder();  
  28.         for (int x = 0; x < 100000; x++) {  
  29.             sb.append(x);  
  30.         }  
  31.   
  32.         long end = System.currentTimeMillis();  
  33.         System.out.println("time:" + (end - start) + "毫秒");  
  34.     }  
  35. }  


-------------------------------Runtime类----------------------------------------------------------------------------------

  1. /**Runtime:程序的运行时类的对象。 
  2.  * 特点:没有构造方法,但是通过单例的方式返回了该类的一个对象。 
  3.  *      public static Runtime getRuntime() 
  4.  */  
  5. public class RuntimeDemo {  
  6.     public static void main(String[] args) {  
  7.         // 获取Runtime类的对象  
  8.         Runtime r = Runtime.getRuntime();  
  9.   
  10.         // 使用功能  
  11.         // public Process exec(String command)  
  12.         try {  
  13.             // r.exec("notepad");  
  14.             r.exec("winmine");  
  15.         } catch (IOException e) {  
  16.             e.printStackTrace();  
  17.         }  
  18.     }  
  19. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值