Random类
java.util.Random类的主要功能时用于产生随机数范例:观察代码
package classku;
import java.util.Random;
public class Randoma {
public static void main(String[] args) {
// TODO Auto-generated method stub
Random rand=new Random();
for(int x=0;x<10;x++) {
System.out.println(rand.nextInt(100)+",");
}
}
}
对象克隆
克隆指的就是对象的赋值操作,在Object类里面有提供有一个clone()方法;clone()方法:protected Object clone() throws CloneNotSupportedException:
要被克隆的对象所在的类需要去实现一个Cloneable接口,但是此接口里面没有包含任何的方法,所以此接口是一个标识接口,表示一种能力。
package classku;
class Person implements Cloneable{
private String name;
private int age;
public Person(String name,int age) {
this.name=name;
this.age=age;
}
protected Object clone() throws CloneNotSupportedException{
return super.clone();//调用父类的对象克隆方法
}
public void setAge(int age) {
this.age=age;
}
public String toString() {
return "姓名:"+this.name+",年龄:"+this.age;
}
}
public class clonea {
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
Person perA=new Person("张上",20);
Person perB=(Person)perA.clone();//克隆
perB.setAge(90);
System.out.println(perA);
System.out.println(perB);
}
}
Math类
Math是一个数学操作类,在Math类之中提供有大量的数学操作方法,我们学一下round()方法
public static long round(double a)
范例:使用四舍五入
package classku;
public class Matha {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(Math.round(13.5));
System.out.println(Math.round(-13.5));
System.out.println(Math.round(-13.51));
}
}
面试题:请说出“Math.round(13.5)”“Math.round(-13.5)”“Math.round(-13.51)”结果是什么?
Math.round(13.5):14
Math.round(-13.5):-13
Math.round(-13.51):-14
Arrays类(理解)
java.util.Arrays.sort() 实际是一个数组排序操作,实际上这一个操作就是调用了java.util包中的Arrays类的sort()方法完成的,而Arrays是一个数组操作的工具类
范例:操作Arrays类
package classku;
import java.util.Arrays;
public class Arraysa {
public static void main(String[] args) {
// TODO Auto-generated method stub
int dataA[]=new int[] {1,2,3};
int dataB[]=new int[] {1,2,3};
System.out.println(Arrays.equals(dataA, dataB));
Arrays.fill(dataA, 9);
System.out.println(Arrays.toString(dataA));
}
}