-------------------------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();
}
}
}