类库(Random类,对象克隆,Math类,Arrays类)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值