黑马程序员——Java工具类概述

来源:互联网 发布:彩票代购源码 编辑:程序博客网 时间:2024/06/01 08:04

--------------  Android培训、Java培训、期待与你交流! ----------------


Java类库为我们提供了大量的工具类,这些工具类中存在很多对程序员编程很有帮助的方法,下面对这些类中的一些做简单的介绍。

1  Arrays

   Arrays类位于java.util包中,该类提供了一系列用于操作数组的静态实用方法。

    1)填充数组fill()方法

  Arrays类有一个作用不是很大的fill()方法,该方法能用同一个值填充整个数组或数组指定的部分,对于引用类型的数组,就是用同一个对象的引用进行填充。fill()方法主要用来填充数组,做测试之用。

   2)数组比较equals()方法

  Arrays类提供了重载的equals()方法,用来比较整个数组,此方法针对所有的基本类型和Object都做了重载。数组相等的条件是元素个数必须相等,并且对应位置处的元素也必须通过equals()方法的比较证明它们也相等。



import java.util.*;public class ArrayComparing{public static void main(String[] args){double[] a1 = new double[10];double[] a2 = new double[10];Arrays.fill(a1,23.1);Arrays.fill(a2,23.1);System.out.println(Arrays.equals(a1,a2));a2[7] = 26.9;System.out.println(Arrays.equals(a1,a2));}}

  3)数组排序sort()

使用Arrays类的sort()方法可以对基本类型的数组进行排序,如果要对对象数组进行排序,该对象必须要实现Comparable接口或者具有相关的Comparator


import java.util.*;public class CompType implements Comparable<CompType>{int i;int j;private static int count = 1;public CompType(int i,int j){this.i = i;this.j = j;}public String toString(){String result = "[i = " + i + ", j = " + j + "]";if(count++%3==0)result += "\n";return result;}public int compareTo(CompType rv){return (i<rv.i?-1:(i==rv.i?0:1));}public static void main(String[] args){CompType[] a = new CompType[]{new CompType(3,7),new CompType(5,9),new CompType(2,56),new CompType(15,45),new CompType(23,156),new CompType(29,1)};System.out.println("before Sorting");System.out.println(Arrays.toString(a));Arrays.sort(a);System.out.println("after Sorting");System.out.println(Arrays.toString(a));}}

   4)数组查找binarySearch()

如果数组已经排序好了,就可以使用Arrays.binarySearch()执行快速查找。但是如果对未排序数组执行binarySearch(),将产生不可预知的结果。


import java.util.*;public class  ArraySearch{public static void main(String[] args) {int[] a = new int[]{32,45,67,123,29,342,254,87,9,123};Arrays.sort(a);System.out.println("Sorted array"+Arrays.toString(a));int location = Arrays.binarySearch(a,9);System.out.println(location+1);}}

2  Collections

    Collections提供了操作容器对象的使用方法,能给我们开发程序提供很大的帮助。

    List排序和查找所使用的方法与对象数组所使用的相应方法有相同的名字和语法,只是用Collections的静态方法替代Arrays中的方法而已。


import java.util.*;public class ListSortAndSearch {public static void main(String[] args) { List<String> list1 = Arrays.asList("one Two three Four five six one".split(" "));List<String> list = new ArrayList<String>(list1); list.addAll(list1); System.out.println(list); Collections.shuffle(list, new Random(47)); System.out.print("Shuffled: " + list); // 用列表迭代器删除最后的元素ListIterator<String> it = list.listIterator(10); while(it.hasNext()) { it.next(); it.remove(); } System.out.println("Trimmed: " + list); Collections.sort(list); System.out.println("Sorted: " + list); String key = list.get(7); int index = Collections.binarySearch(list, key); System.out.println("Location of " + key + " is " + index + ", list.get(" + index + ") = " + list.get(index)); Collections.sort(list, String.CASE_INSENSITIVE_ORDER); System.out.println("Case-insensitive sorted: " + list); key = list.get(7); index = Collections.binarySearch(list, key, String.CASE_INSENSITIVE_ORDER); System.out.println("Location of " + key + " is " + index + ", list.get(" + index + ") = " + list.get(index)); }}[one, Two, three, Four, five, six, one, one, Two, three, Four, five, six, one]Shuffled: [Four, five, one, one, Two, six, six, three, three, five, Four, Two, one, one]Trimmed: [Four, five, one, one, Two, six, six, three, three, five]Sorted: [Four, Two, five, five, one, one, six, six, three, three]Location of six is 7, list.get(7) = sixCase-insensitive sorted: [five, five, Four, one, one, six, six, three, three, Two]Location of three is 7, list.get(7) = three

3  System

Java不支持全局函数和全局变量,Java设计者将一些系统相关的重要函数和变量收集到了一个人统一的类中,这就是System类,该类中所有的成员都是静态的。

  1)exit(int status)

exit(int status)方法的作用是提前终止虚拟机的运行。对于发送异常而想终止虚拟机的运行,传递一个非零值作为参数;对于用户正常操作下,终止虚拟机的运行,传递零值作为参数。

   2)CurrentTimeMillis()

 CurrentTimeMillis()方法返回自197011000秒起至今的毫秒值的时间,该值的类型是long。计算机内部没有真正的日期类型,我们平时用到的日期本质上就是一个数值。该方法经常被用来计算一段代码的运行时间。


4   DateCalendarDateFormat


Date类用于表示日期和时间,用于在设计Date类时没有考虑到国际化,后来又设计了两个新的类CalendarDateFormat类来弥补其不足。

Calendar类是一个抽象类,主要用于完成日期字段之间相互操作的功能,如add()方法可以实现在某一日期的基础上增加若干天,get()方法可以获取日期对象中的年、月、日、时、分、秒等日期字段的值。GregorianCalendar类是目前Calendar类的唯一子类。

import java.util.*;public class DateDemo{public static void main(String[] args){Calendar cale = Calendar.getInstance();System.out.println( cale.get( cale.YEAR)+"年"+( cale.get(cale.MONTH)+1)+"月"+ cale.get( cale.DAY_OF_MONTH)+"日 "+ cale.get( cale.HOUR)+":"+ cale.get( cale.MINUTE)+":"+ cale.get( cale.SECOND)); cale.add( cale.DAY_OF_MONTH,315);System.out.println( cale.get( cale.YEAR)+"年"+( cale.get(cale.MONTH)+1)+"月"+ cale.get( cale.DAY_OF_MONTH)+"日 "+ cale.get( cale.HOUR)+":"+ cale.get( cale.MINUTE)+":"+ cale.get( cale.SECOND));}}

5 Math类与Random


Math类包含了所有用于几何和三角的浮点运算函数,这些函数都是静态的,每个方法都是见名知意的。

Random类是一个伪随机数产生器,随机数是按照某种算法产生的,一旦用一个初值创建了Random类的对象,就可以得到一系列随机数。用相同的初值创建的Random对象,得到的随机数序列是相同的。














0 0
原创粉丝点击