java 学习笔记2022.1.26
排序
Comparator接口
有關(guān)于Comparator接口,這個接口一般是作為參數(shù)放在排序方法里的,最開始我也覺得挺別扭的,后面想了想,這也主要是因為在java中,單獨的一個函數(shù)不能存在,所以設(shè)計了這樣的一個接口,讓我們能通過類來間接調(diào)用函數(shù)
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> {@Overridepublic int compare(Cat a,Cat b){return a.getName().compareTo(b.getName());}}Comparable接口
如何使用,在自己要實現(xiàn)的類中實現(xiàn)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;}@Overridepublic String toString() {return "id='" + id + '\'' +", name='" + name + '\'' +", price=" + price ;}@Overridepublic 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);}} }其實自我感覺這兩個不是特別的難,一個其實就是將判定方法寫在了外面,另一個就是將判定方法寫在了類的內(nèi)部,沒啥難理解的,只是要注意下Comparable接口后<>里自己的類
其他
下面是源碼
//先調(diào)用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);}//直接轉(zhuǎn)換,獲得int值public static int parseInt(String s) throws NumberFormatException {return parseInt(s,10);}可以看出,parseInt直接輸出的原始數(shù)據(jù)而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就是表示前面的字符串是什么進制下的數(shù)字,然后統(tǒng)一轉(zhuǎn)化成十進制
總結(jié)
以上是生活随笔為你收集整理的java 学习笔记2022.1.26的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Rumor CodeForces - 8
- 下一篇: java学习笔记2022.2.11