JAVA常用工具类 联系客服

发布时间 : 星期三 文章JAVA常用工具类更新完毕开始阅读676784045a8102d276a22f3d

一.常用静态方法:

1.搜索

public static int binarySerach(byte[] a,int key)

public static int binarySerach(double[] a,double key) public static int binarySerach(Object[] a, Object key)

在数组a中对key进行二进制搜索,并返回key所在的位置,没有找到,返回负数 2.相等比较

public static boolean equals(byte[] a, byte[] b) public static boolean equals(double[] a, double[] b) public static boolean equals(Object[] a, Object [] b)

比较两个数组,如果数组元素均相同,返回true,否则,返回false

3.填充

public static void fill(byte[] a, byte val) public static void fill(double[] a, double val) public static void fill(Object[] a, Object val) 将数组a中的所有元素填充为val

public static void fill(byte[] a, int fromIndex,int toIndex,byte val) public static void fill(double[] a, int fromIndex,int toIndex ,double val) public static void fill(Object[] a, int fromIndex,int toIndex, Object val) 将数组中a中,从索引fromIndex到toIndex的所有元素填充为val

4.排序

public static void sort(byte[] a) public static void sort(int[] a) public static void sort(long[] a) public static void sort (double[] a, byte val) public static void sort (Object[] a)

将数组中a的所有元素进行升序排序

public static void sort(byte[] a, int fromIndex,int toIndex) public static void sort(int[] a, int fromIndex,int toIndex) public static void sort(long[] a, int fromIndex,int toIndex) public static void sort (double[] a, int fromIndex,int toIndex) public static void sort (Object[] a, int fromIndex,int toIndex)

将数组a中,从索引fromIndex到toIndex-1的所有元素进行升序排序 思考: 如何将数组按降序排序?

System类

一.数组拷贝

static System. arraycopy(Object[] src,int srcPos,Object dest,int destPos,int length) 从源对象数组src的srcPos位置开始取长度为length个元素拷贝到目标数组中 dest 中,并从destPost位置开始存放。 二.标准输入输出有关的属性及其方法:

static PrintStream err; static InputStream in; static PrintStream out; static System.out.print(Object obj); 输出任意对象的字符串表示 static System.out.println(Object obj); 输出任意对象的字符串表示,并换行。 三.系统时间 static System.currentTimeMillis(); 获得当前时间的毫秒数

java.math.BigInteger类

此类用来表示大整型数(即基本数据类型(int,long)无法存储的整数) 一.构造函数 public BigInteger(String val) public BigInteger(String val,int radix)将字符串转换为radix进制整数,然后再创建对象 二.操作方法:

public BigInteger abs(); 返回绝对值

public BigInteger add(BigInteger val); 加 public BigInteger subtract(BigInteger val); 减 public BigInteger multiply(BigInteger val); 乘 public BigInteger divide(BigInteger val); 除 public BigInteger max(BigInteger val); 最大值 public BigInteger min(BigInteger val); 最小值 public BigInteger mod(BigInteger val); 取模 public BigInteger pow(BigInteger val); 幂 public int intValue(); 转换为int 返回 public long longValue();转换为long 返回

Time类和TimerTask类

一.概述:

所在包:java.util

这两个类通常配合完成只执行一次或周期性重复执行的任务。 TimerTask类提供实际要执行的任务。一个实际的任务必须从这个类扩展,并覆盖run 方法(要完成的任务在该方法中实现);

Timer类通过后台线程调度任务,使任务得以执行。 二.Timer类 常用构造方法: Timer() Timer(String name) 常用操作方法: schedule(TimerTask task,int delayRunTime,int interval) task 任务类或其子类 delayRunTime 第一次运行时间(相对于方法该方法调用后), ms

interval 任务被重复执行的时间间隔,ms cancel();//停止时钟

三.TimerTask类 常用构造方法: TimerTask(int maxCounter,Timer aTimer) maxCounter任务被执行的总次数 aTimer Timer对象

常用操作方法: run() 必须被子类覆盖的方法(用于要执行的任务) cancel();//停止任务

四.程序设计要点:

1. 首先从TimerTask扩展一个自己的类

class MyTimerTask extends TimerTask{ private int maxNumberOfTimesToRun=0;//任务被执行的总次数 private Timer myTimer=null;

public MyTimerTask(int maxCounter,Timer aTimer){ super(); maxNumberOfTimesToRun= maxCounter; myTimer=aTimer } //覆盖父类的方法 public void run(){ //要执行的任务代码 } }

2. 启动Timer运行

2.1. 获得一个Timer实例 2.2. 获得TimerTask实例

2.3. 调用schedule()方法使Timer运行。

五.例子:

1.扩展TimerTask

import java.util.*;

class MyTimerTask extends TimerTask{

private int maxNumberOfTimesToRun=0;//任务被执行的总次数 private int runCounter=0;//任务已经被调度的次数 private Timer myTimer=null;

private Random rand=new Random();//用于产生随机数,模拟人物 public MyTimerTask(int maxCounter,Timer aTimer){

super();

maxNumberOfTimesToRun= maxCounter;

}

myTimer=aTimer;

//覆盖父类的方法

public void run(){ }

2启动调度器

public class RunTask {

//要执行的任务代码

if(runCounter

cancel();//停止任务

myTimer.cancel();//停止时钟

long currentTime=System.currentTimeMillis(); Date dt=new Date(currentTime); System.out.print(dt+\);

System.out.println(rand.nextInt(999999));//产生一个随机数 runCounter++;

System.out.println(\任务已经全部完成!\); }

}

public static void main(String[] args){

Timer myTimer=new Timer();//实例化Timer

//实例化TimerTask,任务被执行5次

MyTimerTask task=new MyTimerTask(5,myTimer);

myTimer.schedule(task,0, 3000);//启动时钟 }

接口 java.lang. CharSequence

实现了这个接口的所有类:CharBuffer, String, StringBuffer, StringBuilder

对于使用类型CharSequence作为参数的任何地方可以直接将类型CharBuffer, String, StringBuffer, StringBuilder传递给这个参数。例如: 例如:FileWriter 类追加内容的方法:

fw.append(CharSequence c) 可以这样:fw.append(“大连是一个美丽的海滨城市”);