排序
Comparator接口
-
有关于Comparator接口,这个接口一般是作为参数放在排序方法里的,最开始我也觉得挺别扭的,后面想了想,这也主要是因为在java中,单独的一个函数不能存在,所以设计了这样的一个接口,让我们能通过类来间接调用函数
public class test { public static void main(String[] args) { Cat a= new Cat("b",311,"fjkdjfdkd"); Cat b= new Cat("a",322,"fjkdfjdk"); Cat c= new Cat("c",313,"fjakdfj"); ArrayList<Cat> d = new ArrayList<Cat>(); d.add(a); d.add(b); d.add(c); System.out.println("前"); for(Cat e : d){ System.out.println(e.toString()); } System.out.println("后"); Collections.sort(d,new zimu()); for(Cat e : d){ System.out.println(e.toString()); } } }
public class zimu implements Comparator<Cat> { @Override public int compare(Cat a,Cat b){ return a.getName().compareTo(b.getName()); } }
- 注意下,我们用类实现接口的过程中实现的是compare方法而不是compareTo方法
- 然后排序的过程中,注意要new一个对象实例出来,不然内部的方法是无法被访问的
Comparable接口
-
如何使用,在自己要实现的类中实现Comparable接口就好
public class Goods implements Comparable<Goods>{ private String id; private String name; private double price; public Goods(String id, String name, double price){ this.id=id; this.name=name; this.price=price; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setId(String id) { this.id = id; } public String getId() { return id; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "id='" + id + '\'' + ", name='" + name + '\'' + ", price=" + price ; } @Override public int compareTo(Goods o) { return this.price>o.price ? 1 : -1; } }
public class two { public static void main(String[] args){ Goods one = new Goods("3124","iphone",31232131); Goods two = new Goods("313","tel",314); Goods three = new Goods("411","fks",43143); ArrayList<Goods> list = new ArrayList<Goods>(); list.add(one); list.add(two); list.add(three); System.out.println("first"); for(Goods good : list){ System.out.println(good); } System.out.println("after"); Collections.sort(list); for(Goods good : list){ System.out.println(good); } } }
其实自我感觉这两个不是特别的难,一个其实就是将判定方法写在了外面,另一个就是将判定方法写在了类的内部,没啥难理解的,只是要注意下Comparable接口后<>里自己的类
其他
-
int n= new Double(89.7-34).intValue(); System.out.println(n);
double n = Double.parseDouble("3213"); System.out.println(n);
int n = Integer.parseInt("432432"); System.out.println(n);
int n = Integer.valueOf("4343"); System.out.println(n);
下面是源码
//先调用parseInt获得int值,然后封装成Integer对象,注意封装的逻辑,有缓存 public static Integer valueOf(String s) throws NumberFormatException { return Integer.valueOf(parseInt(s, 10)); } public static Integer valueOf(int i) { assert IntegerCache.high >= 127; if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); } //直接转换,获得int值 public static int parseInt(String s) throws NumberFormatException { return parseInt(s,10); }
可以看出,parseInt直接输出的原始数据而valueOf又对它进行了一次封装;
Description
This method is used to get the primitive data type of a certain String. parseXxx() is a static method and can have one argument or two.
Syntax
Following are all the variants of this method −
static int parseInt(String s) static int parseInt(String s, int radix)
Parameters
Here is the detail of parameters −
- s − This is a string representation of decimal.
- radix − This would be used to convert String s into integer.
Return Value
- parseInt(String s) − This returns an integer (decimal only).
- parseInt(int i) − This returns an integer, given a string representation of decimal, binary, octal, or hexadecimal (radix equals 10, 2, 8, or 16 respectively) numbers as input.
这里radix就是表示前面的字符串是什么进制下的数字,然后统一转化成十进制