美文网首页
OnJava8_比较运算重写

OnJava8_比较运算重写

作者: 啊啊啊哼哼哼 | 来源:发表于2020-01-29 14:43 被阅读0次
  • List的contains方法,当涉及自定义的类对象时,需要重写equals方法;
package collections;

import java.util.ArrayList;
import java.util.List;
class Node{
    private int count;
    Node(int count){
        this.count = count;
    }
//    @Override
@Override
    public boolean equals(Object obj) {
        boolean equal = false;
        if (obj instanceof Node) {
           equal = this.count == ((Node) obj).count;
        }
        return equal;
    }

}
public class ListFeatures {
    public static void main(String[] args){
        List<Integer> lists = new ArrayList<>();
        lists.add(1);
        lists.add(2);
        lists.add(3);
        System.out.println(lists);
        System.out.println(lists.contains(1));
        List<Node> countList = new ArrayList<>();
        countList.add(new Node(1));
        countList.add(new Node(2));
        countList.add(new Node(3));
        System.out.println(countList.contains(new Node(2)));
    }
}

  • 数据存储自定义类对象时并调用sort方法,需要实现Comparator<Weight>
package graph;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;

import static java.util.Arrays.sort;

class Weight {
    int left;
    int right;
    int weight;

    public Weight(int left, int right, int weight) {
        this.left = left;
        this.right = right;
        this.weight = weight;
    }
}

class MyComparator implements Comparator<Weight> {
    @Override
    public int compare(Weight o1, Weight o2) {
        return o1.weight - o2.weight;
    }
}

public class Kruskal {
    static int N = 200010;
    static int M = 400010;
    static Weight[] weights = new Weight[N];
    static int idx = 0;
    static int[] p = new int[N];
    static int sum = 0;
    static int count = 0;

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] line = reader.readLine().split(" ");
        int n = Integer.parseInt(line[0]), m = Integer.parseInt(line[1]);
        for (int i = 1; i <= n; i++) {
            p[i] = i;
        }
        while (m-- > 0) {
            String[] list = reader.readLine().split(" ");
            int a = Integer.parseInt(list[0]), b = Integer.parseInt(list[1]), c = Integer.parseInt(list[2]);
            idx++;
            weights[idx] = new Weight(a, b, c);
        }
        sort(weights, 1, idx, new MyComparator());
        for (int i = 1; i <= idx; i++) {
            Weight tmp = weights[i];
            int left = weights[i].left;
            int right = weights[i].right;
            int we = weights[i].weight;
            int pLeft = find(left);
            int pRight = find(right);
            if (pLeft != pRight) {
                sum += we;
                p[pLeft] = pRight;
                count++;
            } else {
                continue;
            }
            if (count == (n - 1)) {
                break;
            }

        }
        if (count == (n - 1)) System.out.println(sum);
        else System.out.println("impossible");
    }

    private static int find(int left) {
        if (p[left] != left) p[left] = find(p[left]);
        return p[left];
    }

}

  • 优先队列中通过匿名内部类来自定义排序规则
package graph;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

class Node{
    int dis;
    int id;
    public Node(int dis, int id){
        this.dis = dis;
        this.id = id;
    }
    public int getDis() {
        return dis;
    }
    public int getId(){
        return id;
    }
    public void setDis(int dis){
        this.dis = dis;
    }
    public void setId(int id){
        this.id = id;
    }

}

public class Dijkstra2 {
    static  final int Max_length = 0x3f3f3f3f;
    static int n, idx;
    static int N = 100010;
    static int []h = new int[N], e = new int[N], ne = new int[N], w = new int[N];
    static int []dis = new int[N];
    static PriorityQueue<Node> que = new PriorityQueue<Node>(new Comparator<Node>() {
        public int compare(Node o1, Node o2) {
            return o1.getDis() - o2.getDis();
        }
    });
    static boolean[ ] st = new boolean[N];
    public static void main(String[]args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String []line = reader.readLine().split("\\s");
        n = Integer.parseInt(line[0]);
        int m = Integer.parseInt(line[1]);
        while(m-->0){
            String []line1 = reader.readLine().split("\\s");
            int a = Integer.parseInt(line1[0]), b = Integer.parseInt(line1[1]), c = Integer.parseInt(line1[2]);
            add(a,b,c);
        }
        bfs();
    }

    private static void bfs() {
        for(int i =1;i<=n;i++){
            dis[i] = Max_length;
        }
        dis[1] = 0;
        que.offer(new Node(0,1));
        while(que.size()>0){
            Node tmp = que.poll();
            int distance = tmp.getDis();
            int id = tmp.getId();
            if(st[id]) continue;
            st[id] = true;
            for(int i =h[id]; i!=0;i = ne[i]){
                int j = e[i];
                if(dis[j]>distance+w[i]) {
                    dis[j] = distance+w[i];
                    que.offer(new Node(dis[j],j));
                }
            }

        }
        if (dis[n] == 0x3f3f3f3f) System.out.println("-1");
        else System.out.println(dis[n]);

    }

    private static void add(int a, int b, int c) {
        idx++;
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx;

    }

}

相关文章

  • OnJava8_比较运算重写

    List的contains方法,当涉及自定义的类对象时,需要重写equals方法; 数据存储自定义类对象时并调用s...

  • 比较运算

  • 比较运算

    比较运算 基础类型比较 对于int、bool等基础类型直接比较值相等,对于指针则是直接比较的指针所指向的地址是否相...

  • c#10月25号

    课上学习了主要学习了运算符重载自定义转换以及方法的隐藏,重写,感觉比较难的是自定义的转换。 下午练习了类和运算符重...

  • 继承 方法的重写和运算符重载

    重写 添加属性 运算符的重载 内存管理 包的使用

  • MySQL 中的运算符和常用函数

    MySQL学习笔记(3) 运算符 类型:算术、比较、逻辑和位运算符 算术运算符 比较运算符 比较运算符可比较数字、...

  • 运算 & 运算符

    运算 & 运算符 算术运算 比较运算 比较运算的结果为bool值 赋值运算 逻辑运算 位运算 二进制的运算 身份检...

  • 逻辑运算符

    赋值运算符 比较运算符 先运算再比较 逻辑运算符 比较大于逻辑!,&&,|| 单目运算符(!++ -- & ) 大...

  • 2019-03-06

    运算符:数学运算符、比较运算符、逻辑运算符、赋值运算符 2.比较运算符:>,<,==,!=,>=,<= 所有比较运...

  • 逻辑运算,闭包

    算术运算(+-*/%)>比较运算(!==,>= ,<= , = =)>逻辑运算(&&,||)>赋值运算(=)...

网友评论

      本文标题:OnJava8_比较运算重写

      本文链接:https://www.haomeiwen.com/subject/gpwvthtx.html