/**
* Math类、Random类、Arrays类:具体查JAVA手册。。。。。。
*/
public class Main {
public static void main(String[] args) {
String[] s1 = {"a","b","c","d","e"};
String[] s2 = {"a","b","c","d","e"};
boolean b = Arrays.equals(s1,s2); //比较两数组内容
System.out.println(b); //输出true String[] s3 = new String[6];
Arrays.fill(s3,"井空");
System.out.println(s3.length);
for(String i:s3) System.out.print(i+" "); System.out.println(); String[] s4 = Arrays.copyOfRange(s1, 2, 3); //[2,3)
for(String i:s4) System.out.print(i+" "); System.out.println(); int index = Arrays.binarySearch(s1, "c");
System.out.println(index);
}
}
/**
* 日期操作类
* Date类:表示特定的瞬间,精确到毫秒,也是程序运行时的当前时间
* Calendar类:可以精确到毫秒
* DateFormat类
* SimpleDateFormat类:日期格式化类
*/
public class Main {
public static void main(String[] args) {
Date d1 = new Date();
Date c = new Date();
System.out.println(d1.getTime()); //毫秒表示
System.out.println(d1); //格式表示 //两种实例化方式
Calendar ca1 = Calendar.getInstance();
Calendar ca2 = new GregorianCalendar(); //阳历
int year = ca1.get(Calendar.YEAR);
int month = ca1.get(Calendar.MONTH)+1;
int day = ca1.get(Calendar.DAY_OF_MONTH);
int hour = ca1.get(Calendar.HOUR);
int minute = ca1.get(Calendar.MINUTE);
int second = ca1.get(Calendar.SECOND);
System.out.println(year+" "+month+" "+day);
System.out.println(hour+" "+minute+" "+second); DateFormat df1 = DateFormat.getDateInstance();
DateFormat df2 = DateFormat.getTimeInstance();
String nowDate = df1.format(new Date());
String nowTime = df2.format(new Date());
System.out.println(nowDate);
System.out.println(nowTime); SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy^MM^dd HH:mm:ss SSS毫秒");
String swa = sdf1.format(new Date());
System.out.println(swa);
}
}
/**
* 对象比较器:对两个或多个数据项进行比较,以确定它们是否相等,或确定它们之间的大小关系
* Comparable接口:强行对实现它的每个类的对象进行整体排序,类的compareTo方法被称为它的自然比较方法
* 如果自定义类的对象要实现排序,那么就要实现Comparable接口,并实现接口的CompareTo方法
*
* Comparable接口要求自定义类去实现,按照OO原则:对修改关闭,对扩展开放。
* 如果这个类已经定义好,不想再去修改它,如何实现? 答:Arrays.sort有一个重载方法:
*/
public class Main {
public static void main(String[] args) {
String[] names = {"a","b","c","d","e","f"};
Person[] persons = {
new Person("a",10), new Person("b",9),
new Person("c",8), new Person("d",7),
new Person("e",6), new Person("f",5),
};
//自定义类对对象数组排序
Arrays.sort(persons);
for(Person i:persons) System.out.println(i.name+" "+i.age); Person2[] persons2 = {
new Person2("a",10), new Person2("b",9),
new Person2("c",8), new Person2("d",7),
new Person2("e",6), new Person2("f",5),
};
Arrays.sort(persons2,new Person2Comparator()); //使用自定义的比较器
for(Person2 i:persons2) System.out.println(i.name+" "+i.age);
System.out.println(Arrays.toString(persons2));//返回数组内容的字符串表示形式
}
} class Person implements Comparable<Person>{
public String name;
public int age;
Person(String name,int age){
this.name = name;
this.age = age;
}
@Override
public int compareTo(Person o) { //比较器的比较方法
//实现:定义对象之间的比较规则
if(o==null){
throw new NullPointerException();
}
//如果当前对象小于参数对象,返回-1
//如果当前对象等于参数对象,返回0
//如果当前对象大于参数对象,返回1
if(this.age < o.age)
return -1;
else if(this.age < o.age)
return 1;
return 0;
}
} class Person2{
public String name;
public int age;
Person2(String name,int age){
this.name = name;
this.age = age;
}
}
class Person2Comparator implements Comparator<Person2>{
@Override
public int compare(Person2 o1, Person2 o2) {
if(o1==null || o2==null)
throw new NullPointerException();
if(o1.age < o2.age)
return -1;
else if(o1.age > o2.age)
return 1;
return 0;
}
}
/**
* 对象的克隆:将一个对象复制一份(克隆要比new效率高)
* 一个类要想实现克隆功能,必须:
* 1.实现Cloneable接口,该接口必须是一个标记接口
* 2.重写Obejct类中的Clone方法
*/
public class Main {
public static void main(String[] args) {
Cat c1 = new Cat("a",10);
System.out.println("c1="+c1);
try{
Cat c2 = (Cat)c1.clone();
System.out.println("c2="+c2);
}catch(CloneNotSupportedException e){
e.printStackTrace();
}
}
} class Cat implements Cloneable{
private String name;
private int age; public Cat(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Cat{" + "name=" + name + ", age=" + age + '}';
}
@Override
//重写Obejct的Clone方法
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
/**
* 二叉树
*/
public class Main {
public static void main(String[] args) {
BinaryTree t1 = new BinaryTree();
t1.add(8);
t1.add(9);
t1.add(3);
t1.add(4);
t1.print();
}
} class BinaryTree{
private Node root; public void add(int data){
if(root==null){
root = new Node(data);
}else{
root.addNode(data);
}
}
public void print(){
if(root!=null){
root.printNode();
}else{
System.out.println("NULL");
}
} class Node{
private int data;
private Node left;
private Node right;
public Node(int data){
this.data = data;
}
public void addNode(int data){
if(data <= this.data){
if(this.left==null){
this.left = new Node(data);
}else{
this.left.addNode(data);
}
}else{
if(this.right==null){
this.right = new Node(data);
}else{
this.right.addNode(data);
}
}
}
public void printNode(){
System.out.println(this.data);
if(this.left!=null)
this.left.printNode();
if(this.right!=null)
this.right.printNode();
}
}
}