黑马程序员: Collections、Arrays、Runtime、Random类的学习
来源:互联网 发布:静态网页制作软件 编辑:程序博客网 时间:2024/05/22 17:16
------- android培训、java培训、期待与您交流! ----------
/*----------------------------Arrays工具类--------------------------- * Arrays:是用来操作数组的工具类。 * 方法都是静态的,通过类名直接使用。 * * 功能: * public static String toString(int[] a):把数组转成字符串形成 * public static void sort(int[] a):对数组进行排序 * public static int binarySearch(int[] a, int key):二分查找法 */public class ArraysDemo {public static void main(String[] args) {// 定义一个数组int[] arr = { 45, 37, 94, 82, 63 };// 需求:写一个功能实现把数组转成字符串 格式:[元素1,元素2,元素3...]// String s = arrayToString(arr);String s = Arrays.toString(arr);System.out.println("s:" + s);// 排序Arrays.sort(arr);System.out.println("arr:" + Arrays.toString(arr));// 二分查找法int index = Arrays.binarySearch(arr, 82);System.out.println("index:" + index);}public static String arrayToString(int[] arr) {StringBuilder sb = new StringBuilder();sb.append("[");// 遍历数组,追加到sb中。for (int x = 0; x < arr.length; x++) {if (x == arr.length - 1) {sb.append(arr[x]);} else {sb.append(arr[x]).append(",");}}sb.append("]");return sb.toString();}} -------------------------------Collections工具类--------------------------- Collections:是用来操作Collection集合的工具类。 * * 面试题:Collection和Collections的区别? * Collection是接口,定义了Collection集合的共性内容。 * Collections是操作Collection集合类的工具类。 * * 要掌握的功能: * public static int binarySearch(List list, T key) * public static T max(Collection coll) * public static void reverse(List list) * public static void shuffle(List list) * public static void sort(List list) * public static List synchronizedList(List list) */public class CollectionsDemo {public static void main(String[] args) {// 创建集合对象List<Integer> list = new ArrayList<Integer>();// 这样做完以后,list就是线程安全的。// List<Integer> list = Collections.synchronizedList(new// ArrayList<Integer>());// 添加元素list.add(9);list.add(5);list.add(4);list.add(6);list.add(7);list.add(8);list.add(3);// public static T max(Collection coll)// Integer i = Collections.max(list);// System.out.println("i:" + i);// public static void reverse(List list)// Collections.reverse(list);// public static void sort(List list)// Collections.sort(list);// public static int binarySearch(List list, T key)// int index = Collections.binarySearch(list, 6);// System.out.println("index:" + index);// public static void shuffle(List list)// 每次调用,随机把集合中的数据排序。洗牌Collections.shuffle(list);System.out.println("list:" + list);}}/* * 模拟扑克牌的洗牌。 * * 思路: * 1:创建一副新牌 * 黑桃 A,2,3,4,...K * 红桃 A,2,3,4,...K * 梅花 A,2,3,4,...K * 方块 A,2,3,4,...K * 2:通过分析,我们发现每张牌用字符串类型接受。 * 3:创建一个花色数组,创建一个牌大小数组。 * 4:创建一个集合,把两个字符中的数据按照牌进行拼接,然后存入集合中。加入大小鬼。 * 5:模拟发牌 */public class CardDemo {public static void main(String[] args) {// 创建一副新牌// 创建花色String[] colors = { "黑桃", "红桃", "梅花", "方块" };// 创建牌String[] cards = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10","J", "Q", "K" };// 创建集合ArrayList<String> array = new ArrayList<String>();for (int x = 0; x < colors.length; x++) {for (int y = 0; y < cards.length; y++) {// 创建牌,并加入集合array.add(colors[x].concat(cards[y]));}}// 加入大小王array.add("大王");array.add("小王");// 洗牌Collections.shuffle(array);System.out.println(array);}}------------------------------------System系统类--------------------------- System:系统类,提供一些类字段和方法供我们使用。 * * 要掌握的功能: * public static void exit(int status):退出jvm。根据惯例,非0的状态码表示异常终止。 * public static Properties getProperties():获取系统的属性。 * public static long currentTimeMillis():获取当前时间的毫秒值。测试程序的运行时间。 */public class SysetmDemo {public static void main(String[] args) {// System.out.println("haha");// System.exit(0);// Properties prop = System.getProperties();// Set<Object> set = prop.keySet();// for (Object key : set) {// Object value = prop.get(key);// System.out.println(key + "***" + value);// }// 测试:long start = System.currentTimeMillis();// String s = "";// for (int x = 0; x < 100000; x++) {// s += x;// }StringBuilder sb = new StringBuilder();for (int x = 0; x < 100000; x++) {sb.append(x);}long end = System.currentTimeMillis();System.out.println("time:" + (end - start) + "毫秒");}}------------------------------------Runtime类--------------------------------- Runtime:程序的运行时类的对象。 * 特点:没有构造方法,但是通过单例的方式返回了该类的一个对象。 * public static Runtime getRuntime() */public class RuntimeDemo {public static void main(String[] args) {// 获取Runtime类的对象Runtime r = Runtime.getRuntime();// 使用功能// public Process exec(String command)try {// r.exec("notepad");r.exec("winmine");} catch (IOException e) {e.printStackTrace();}}}-----------------------------Random:用于产生随机数的类构造方法:Random():默认以系统时间作为随机数生成器的种子Random(long seed):以固定的Seed参数作为随机数生成器的种子功能方法: int nextInt(); int 范围内的值 int nextInt(int n):[0-n)之间的值public static void main(String[] args) {// 创建随机数对象// Random r = new Random();Random r = new Random(17);// 获取随机数// for (int x = 0; x < 10; x++) {// System.out.println(r.nextInt());// }System.out.println("******************");for (int x = 0; x < 10; x++) {System.out.println(r.nextInt(100));}}
------- android培训、java培训、期待与您交流! ----------
- 黑马程序员: Collections、Arrays、Runtime、Random类的学习
- 黑马程序员--Java笔记11--静态类(Collections、Arrays、System、Runtime、Date、Calendar、Math)
- 黑马程序员_JAVA集合框架的工具类:Collections、Arrays
- 黑马程序员-Collections和Arrays工具类
- 黑马程序员-工具类Collections&Arrays
- 黑马程序员_<<Collections,Arrays>>
- 黑马程序员14. Collections&Arrays
- 黑马程序员--Collections与Arrays
- 黑马程序员---泛型,Collections,Arrays
- 黑马程序员--------java Collections、Arrays、高级for、可变参数、静态导入、System、Runtime、Math、Date、Calendar
- 黑马程序员—java基础学习--Map集合、Collections,Arrays工具类
- 黑马程序员java学习笔记——Collections、Arrays工具类和其他对象
- 黑马程序员 + 第17天 Collections类和Arrays类
- 黑马程序员--第十七天:Collections 类, Arrays 类
- 黑马程序员——Collections类 Arrays类
- 黑马程序员—Java工具类(Collections、Arrays)
- 黑马程序员-day17工具类Collections和Arrays
- 黑马程序员--泛型、集合框架工具类:collections和Arrays
- Oracle 递归查询
- hadoop 伪分布式安装
- c++窗口程序模板
- C语言相关问题
- 判断单链表是否存在环链
- 黑马程序员: Collections、Arrays、Runtime、Random类的学习
- 黑马程序员_初识java
- 结合第二堂课的知识所回答的一些小问题
- 黑马程序员_java 正则表达式
- 地址类型的强制转换
- java面试过程中经常遇到的面试题
- Ubuntu下调整swap分区的大小
- 卷积的意义
- diff和patch命令