日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 学习笔记2022.1.26

發布時間:2024/4/18 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 学习笔记2022.1.26 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

排序

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> {@Overridepublic 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;}@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);}} }
  • 其實自我感覺這兩個不是特別的難,一個其實就是將判定方法寫在了外面,另一個就是將判定方法寫在了類的內部,沒啥難理解的,只是要注意下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就是表示前面的字符串是什么進制下的數字,然后統一轉化成十進制

  • 總結

    以上是生活随笔為你收集整理的java 学习笔记2022.1.26的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。