确定两个参与者的equals()方法不起作用

SHR*_*SHR 2 java equals

我究竟做错了什么?

问题是我将如何包含一个equals()方法,Participant如果它们在所有三个字段中具有相同的值,则确定两个s相等?包括为每个字段分配参数值的构造函数以及toString()返回包含所有值的String 的方法.

import javax.swing.JOptionPane;
import java.util.*;

public class Abc {

    private static Ab mini[] = new Ab[2];
    private static Ab diving[] = new Ab[2];

    public static void main(String[] args) {

        String name = "";
        String add = "";
        int age = 0;

        Ab p = new Ab(name, age, add);
        Ab p1 = new Ab(name, age, add);

        setParticipant();
        setParticipant1();

        displayDetail();
        displayDetail1();

        if (p == p1) {

            System.out.println("equal");
        } else {
            System.out.println("not equal");

        }

    }

    public static void setParticipant() {
        for (int x = 0; x < mini.length; x++) {
            System.out.println("Enter detail about participant1 " + (x + 1) + "...");

            String name = getName();
            String add = getAdd();
            int age = getAge();

            System.out.println();
            mini[x] = new Ab(name, age, add);
            //Create the object with the data you collected and put it into your array.

        }
    }

    public static void setParticipant1() {
        for (int y = 0; y < diving.length; y++) {
            System.out.println("Enter Participant2 " + (y + 1) + "...");

            String name = getName();
            String add = getAdd();
            int age = getAge();
            System.out.println();
            //mini[x] = new Ab(name, age, add);
            //Create the object with the data you collected and put it into your array.
            diving[y] = new Ab(name, age, add);
        }
    }

    public static void displayDetail() {
        // for (int y = 0; y < diving.length; y++) {
        System.out.println("Name \tAge \tAddress");

        for (int x = 0; x < mini.length; x++) {
            System.out.println(mini[x].toString());
            // System.out.println(diving[y].toString());
        }
    }

    public static void displayDetail1() {
        System.out.println("Name \tAge \tAddress");

        for (int y = 0; y < diving.length; y++) {
            System.out.println(diving[y].toString());
        }
    }

    public static String getName() {
        Scanner sc = new Scanner(System.in);
        String name;

        System.out.print(" Participant name: ");
        return name = sc.next();
    }

    // System.out.print(" Participant name: ");
    // name = sc.next();
    public static int getAge() {
        int age;
        System.out.print(" Enter age ");
        Scanner sc = new Scanner(System.in);
        return age = sc.nextInt();
    }

    public static String getAdd() {
        String add;
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter Address: ");
        return add = sc.next();
    }
}
Run Code Online (Sandbox Code Playgroud)
public class Ab {

    private String name;
    private int age;
    private String address;

    public Ab(String strName, int intAge, String strAddress) {
        name = strName;
        age = intAge;
        address = strAddress;
    }

    @Override
    public String toString() {
        return name + " " + age + " " + address;
    }

    public boolean equals(Participant value) {
        boolean result;

        if (name.equals(name) && age == value.age && address.equals(address)) {
            result = true;
        } else {
            result = false;
        }

        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

Era*_*ran 6

equals应该覆盖Objectequals方法,这意味着它应该接受Object一个参数.

更改

public boolean equals(Participant value)
Run Code Online (Sandbox Code Playgroud)

@Override
public boolean equals(Object value)
Run Code Online (Sandbox Code Playgroud)

哦,equals如果你不使用它,你无法覆盖它.

更改

p==p1
Run Code Online (Sandbox Code Playgroud)

p.equals(p1)
Run Code Online (Sandbox Code Playgroud)