android培训——我的java笔记,期待与您交流!
对象比较
基本类型的比较
java当中想要排序可以使用 Arrays.sort()
方法
如下
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
public class DateDemo {
public static void main(String[] args) {
int[] arrs = {4,6,8,2,1,5};
Arrays.sort(arrs);
for(int a:arrs){
System.out.print(a+" ");
}
System.out.println("");
}
}
输入结果就是 1 2 4 5 6 8
然而自己创建的类想要比较需要些什么呢
引用类型比较
public class DateDemo {
public static void main(String[] args) {
Lollipop[] ls = {new Lollipop(4, "a", 0.5),new Lollipop(10, "b", 2.5),
new Lollipop(8, "c", 5.0),new Lollipop(12, "d", 8.5)};
for(Lollipop l:ls){
System.out.println(l);
}
Arrays.sort(ls);
}
}
输出还是没问题的,不过排序就出现了问题。
Exception in thread “main” java.lang.ClassCastException: chen.Lollipop cannot be cast to java.lang.Comparable
at java.util.ComparableTimSort.countRunAndMakeAscending(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at chen.DateDemo.main(DateDemo.java:13)
先挑一个自己的代码看看问题,发现时sort方法出现了问题,经过一系列源码查找
if (length < INSERTIONSORT_THRESHOLD) {
for (int i=low; i<high; i++)
for (int j=i; j>low &&
((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
swap(dest, j, j-1);
return;
}
有一个奇怪的转型(Comparable),这个就是这次笔记的主要内容,写好的Lollipop类并没有做Comparable接口
Comparable 比较器接口
在帮助文档中查找发现该接口被许多类实现,当然包括先前的int类型,所以基本类型排序并没有出现问题。
比较方法就是comparabTo()
方法
下面就尝试实现接口
public class Lollipop implements Comparable<Lollipop> {
private int number;
private String type;
private double price;
public Lollipop(int number, String type,double price){
this.number = number;
this.type = type;
this.price = price;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Lollipop [number=" + number + ", type=" + type + ", price="
+ price + "]";
}
/*
* @return 返回-1:当前值小于比较值, 返回1当前值大于比较值,返回0两者相等
* @param 接受一个Lollipop类型参数
*/
@Override
public int compareTo(Lollipop l) {
if(this.price < l.price){
return -1;
}else if(this.price > l.price){
return 1;
}else{
return 0;
}
}
}
接口实现以后就能比较啦输出
比较之后Lollipop [number=4, type=a, price=0.5]
比较之后Lollipop [number=10, type=b, price=2.5]
比较之后Lollipop [number=8, type=c, price=5.0]
比较之后Lollipop [number=12, type=d, price=8.5]
比较完成~
二叉树的实现
首先二叉树的每一个节点都可以有左节点和右节点,存数据的时候数据比根节点小则放左节点,大则放右节点。
所以目前实现的二叉树类应该是这样的
- 二叉树类
- 根节点
- 添加节点方法
- 打印方法
- 内部的节点类
- 当前节点存储的数据
- 左节点
- 右节点
- 构造方法
- 添加节点方法
- 节点打印方法
按着这个写二叉树类
public class BinaryTree {
private Node root;
public void addNode(int data){
if(root == null){
root = new Node(data);
}else{
root.add(data);
}
}
public void printNode(){
if(root != null){
root.print();
}
}
private class Node{
private int data;
private Node left;
private Node right;
public Node(int data){
this.data = data;
}
//添加节点
public void add(int data){
if(this.data > data){
if(this.left == null){
this.left = new Node(data);
}else{
this.left.add(data);
}
}else if(this.data < data){
if(this.right == null){
this.right = new Node(data);
}else{
this.right.add(data);
}
}
}
//按照中序遍历,左——根——右
public void print(){
if(this.left != null){
this.left.print();
}
System.out.print(this.data+"-->");
if(this.right != null){
this.right.print();
}
}
}
好了,来输出一下试试
import java.util.Random;
public class BinaryTreeDemo {
public static void main(String[] args) {
BinaryTree bt = new BinaryTree();
for(int i = 0; i < 10; i++){
Random r = new Random();
int temp = r.nextInt(20);
bt.addNode(temp);
System.out.print(temp+" ");
}
System.out.println();
bt.printNode();
}
}
结果:
18 1 9 17 17 3 10 19 15 12
1–>3–>9–>10–>12–>15–>17–>17–>18–>19–>
按照中序输出之后就是由大到小的顺序了。