天天看點

一些Java刷題時的小知識點

1. 重寫Arrays.sort

Arrays.sort預設按升序進行排序。降序排列需要自己實作Comparator接口。而且有時候有兩個坐标的時候的排序,也需要實作Comparator接口。

public static void main(String[] args) {

       class point {
           private int x;
           private int y;
           public point(int x, int y) {
               this.x = x;
               this.y = y;
           }
       }
       point[] points = new point[5];
       Double d;
       int r_x, r_y;
       for (int i = 0 ; i < 5; i++) {
           d = Math.random();
           r_x = (int)(d * 10);
           d = Math.random();
           r_y = (int)(d * 10);
           points[i] = new point(r_x, r_y);
       }
       Arrays.sort(points, new Comparator<point>() {
           @Override
           public int compare(point o1, point o2) {
               if (o1.x == o2.x)
                   return o2.y - o1.y;  //按y降序排列
               return o1.x - o2.x;   //按x升序排列
           }
       });
       for (point p : points)
           System.out.println("[" + p.x +"," + p.y + "]");

       int[][] nums = {{4,3},{2,7},{8,1}};
       Arrays.sort(nums, new Comparator<int []>() {
           public int compare(int[] a, int[] b) {
               if (a[0] == b[0])
                   return a[1] - b[1];
               else
                   return a[0] - b[0];
           }
       });      

View Code

2.對容器進行排序:

Collections.sort(S);

Arrays.sort() //預設降序排序

Arrays.sort(T[], Collections.reversOrder()) //升序

Arrays.sort(T[], (a, b) -> a - b);

 Arrays.sort(T[], java.util.Comparator) //自己寫類實作comparator接口

3. queue的使用

Queue<String> queue = new LinkedList<String>();

  • priority queue的使用:

建立大頂堆:

  Comparator<Integer> comparator = new Comparator<Integer>() {

@Override
    public int compare(Integer o1, Integer o2) {
        return o1 - o2; //生成最大堆使用o2-o1,生成最小堆使用o1-o2
    }
};

PriorityQueue<Integer> pq = new PriorityQueue<>(k, comparator);      
  • priority queue中為HashMap元素,怎麼根據map的value進行排序?生成小頂堆:

Map.Entry是Map聲明的一個内部接口,此接口為泛型,定義為Entry<K,V>。它表示Map中的一個實體(一個key-value對)

PriorityQueue<Map.Entry<String, Integer>> pq = new PriorityQueue<>(
                 (a,b) -> a.getValue()==b.getValue() ? a.getKey().compareTo(b.getKey()) : a.getValue()-b.getValue() );           

4.輸出數組:

System.out.println(Arrays.toString(nums));

5.int string的互相轉換

int to string: String x = Integer.toString(x); 

string to int: int x = Integer.parseInt(x);

6.快排模版(死記都要記住!)(以及二叉樹中序、後序非遞歸周遊模版)

public static void quickSort(int[] nums) {
        recurpartition(nums, 0, nums.length - 1);
    }


    private static void recurpartition(int[] nums, int low, int high) {
        if (high - 1 < low || low >= high)
            return;
        int part = partArray(nums, low, high);
        recurpartition(nums, low, part - 1);
        recurpartition(nums, part + 1, high);
    }

    private static int partArray(int[] nums, int low, int high) {
        int pivot = nums[low];
        while (low < high) {
            while (low < high && nums[high] > pivot) high--;
            nums[low] = nums[high];
            while (low < high && nums[low] < pivot) low++;
            nums[high] = nums[low];
        }
        nums[low] = pivot;
        return low;
    }      

View Code

7. Java中:

  • 1.length()方法是針對字元串來說的,要求一個字元串的長度就要用到它的length()方法;
  •  2.length屬性是針對Java中的數組來說的,要求數組的長度可以用其length屬性;
  •  3.java中的size()方法是針對泛型集合說的,如果想看這個泛型有多少個元素,就調用此方法來檢視!

轉載于:https://www.cnblogs.com/shawshawwan/p/7641238.html