if(!age1.equals(age2))中!的意义是什么
时间: 2024-05-25 15:12:22 浏览: 8
在这个代码中,符号 "!" 表示逻辑运算符 "非",其作用是对布尔值进行取反操作。具体来说,"!" 的作用是将括号中的表达式的值取反,即如果括号中的表达式的值为 true,则取反后的值为 false;如果括号中的表达式的值为 false,则取反后的值为 true。
因此,如果写成 if(!age1.equals(age2)),则表示如果 age1 不等于 age2,则执行 if 语句内部的代码。相反,如果写成 if(age1.equals(age2)),则表示如果 age1 等于 age2,则执行 if 语句内部的代码。
相关问题
分析此代码的运行import java.util.ArrayList; import java.util.List; import java.util.Scanner; class Test{ public static void main(String[] args) { Scanner scan=new Scanner(System.in); String content=""; List<Student> students=new ArrayList<>(); while(!"".equals(content=scan.nextLine())){ String[] datas=content.split(","); Student stu=new Student(datas[0],Integer.parseInt(datas[2]),datas[1]); if (!students.contains(stu)){ students.add(stu); } } for(Student stu:students) System.out.println(stu); } } class Student { private String stuNo; private int age; private String name; @Override public String toString() { return "Student{" + "stuNo='" + stuNo + '\'' + ", age=" + age + ", name='" + name + '\'' + '}'; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((stuNo == null) ? 0 : stuNo.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (stuNo == null) { if (other.stuNo != null) return false; } else if (!stuNo.equals(other.stuNo)) return false; return true; } public Student(String stuNo, int age, String name) { super(); this.stuNo = stuNo; this.age = age; this.name = name; } }
该代码的功能是从标准输入中读取多个字符串,每个字符串表示一个学生的信息(格式为"学号,姓名,年龄"),将这些学生信息存储在一个List容器中,并输出List中的所有学生信息(按照学号升序排列)。
具体来说,代码首先创建了一个Scanner对象,用于读取标准输入。然后定义了一个空字符串content和一个List容器students。接下来进入一个while循环,每次循环读取一行字符串并将其保存在content中,如果content不为空,则将其按照逗号分隔为三部分(学号、姓名、年龄),并创建一个Student对象,然后判断students中是否已经包含该Student对象,如果不包含,则将其添加到students中。当读取到空字符串时,循环结束。最后,代码通过一个for-each循环遍历students中的每个Student对象,并调用它的toString()方法输出学生信息。
Student类是一个简单的JavaBean类,包含学号、姓名、年龄三个属性,重写了toString()方法,以便输出学生信息。此外,还重写了hashCode()和equals()方法,以便判断两个Student对象是否相等。其中,hashCode()方法使用了Java语言规范推荐的计算hash值的方式,而equals()方法则比较两个Student对象的学号是否相等。
总体来说,该代码的逻辑清晰,实现简单,但需要注意输入的格式必须符合要求,否则可能会出现异常。同时,在判断两个Student对象是否相等时,只比较了学号,如果存在两个学生学号相同但其他信息不同的情况,该代码可能会出现错误。
public int hashCode() { return Objects.hash(name, age); } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || getClass() != obj.getClass()) { return false; } Person person = (Person) obj; return age == person.age && Objects.equals(name, person.name); } }这段代码详细解答
这段代码是一个自定义类 `Person` 实现了 `hashCode()` 和 `equals()` 方法,以使其可以作为 `HashMap` 的键使用。
`hashCode()` 方法通过 `Objects.hash(name, age)` 计算出 `Person` 对象的哈希值,这里将 `name` 和 `age` 两个字段传入 `Objects.hash()` 方法,该方法会为它们生成一个哈希值。这个哈希值将用来定位 `Person` 对象在 `HashMap` 中的桶位置。
`equals()` 方法是用来比较两个 `Person` 对象是否相等的。首先,如果两个对象的引用地址相等,则它们一定相等,直接返回 `true`。如果对象为 `null` 或者它们的类不同,则它们一定不相等,直接返回 `false`。如果两个对象的类相同,则将它们转换为 `Person` 类型,比较它们的 `name` 和 `age` 字段是否相等,如果相等则返回 `true`,否则返回 `false`。
这两个方法的实现非常重要,因为它们是 `HashMap` 底层使用的关键方法,如果两个对象的哈希值不相等或者 equals() 方法比较不正确,就会导致 `HashMap` 查找和修改元素时出现错误。因此,自定义类作为 `HashMap` 的键时,一定要正确地实现这两个方法。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)