java中,如何判断List<User>中的是否包含User,User类有两个属性:userName,userPassword,给一个代码
时间: 2024-05-08 17:20:04 浏览: 22
示例:
可以通过调用List的contains方法来判断List中是否包含某个对象。针对User对象,可以自己实现一个equals方法来比较对象中的属性是否相同。
示例代码如下:
```
public class User {
private String userName;
private String userPassword;
public User(String name, String password) {
userName = name;
userPassword = password;
}
// 重写equals方法,使其比较两个对象中的属性是否相同
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof User)) {
return false;
}
User u = (User) obj;
return this.userName.equals(u.userName) && this.userPassword.equals(u.userPassword);
}
}
// 在List中查找是否包含某个User对象
List<User> users = new ArrayList<>();
users.add(new User("Alice", "123"));
users.add(new User("Bob", "456"));
User targetUser = new User("Alice", "123");
if (users.contains(targetUser)) {
System.out.println("List中包含目标User对象");
} else {
System.out.println("List中不包含目标User对象");
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
<%@ page import="java.util.List" %> <%@ page import="com.example.model.User" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <title>Title</title> User List Id Username password Email Gender Birthday <c:choose> <c:when test="${empty userlist}"> No Order Found. </c:when> <c:otherwise> <c:forEach var="u" items="${userlist}"> User :<%=u.getUsername()%> Password :<%=u.getPassword()%> Email :<%=u.getEmail()%> Sex :<%=u.getSex()%> Country :<%=u.getCountry()%> Edit Delete </c:forEach> </c:otherwise> </c:choose> Add User
![](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)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<body>
<input type="button" value="新增" onclick="add()">
ID
姓名
年龄
密码
修改 删除
<c:if test="${not empty list }">
<c:forEach items="${list }" var="user" varStatus="s">
${s.count}
${user.username}
${user.age}
${user.password}
修改
删除
</c:forEach>
</c:if>
<script >function add(){
location.href="add.jsp";
}
function updatemethod(userid) {
var id = parseInt('<%= session.getAttribute("id") %>');
if (userid != id) {
alert("你不能修改别人的信息");
} else {
window.location.href = "update.jsp";
}
}
function deletemethod(userid) {
var id = parseInt('<%= session.getAttribute("id") %>');
if (userid != id) {
alert("你不能删除别人的信息");
} else {
if(confirm("你确定要删除自己的信息吗?")) {
window.location.href = "index.jsp";
}
}
}</script>
</body>
</html>上述代码哪里报错,要如何修改
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)