首页 > 其他分享 >存入三个用户对象,根据id查找用户信息1.存在则返回true 2.存在则返回该用户(索引)

存入三个用户对象,根据id查找用户信息1.存在则返回true 2.存在则返回该用户(索引)

时间:2023-03-20 11:44:43浏览次数:37  
标签:返回 String list 用户 public User password true id

package com.itheima.test;

//创建个User类
public class User {
    private String id;
    private String username;
    private String password;

    public User() {
    }

    public User(String id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    /**
     * 获取
     * @return id
     */
    public String getId() {
        return id;
    }

    /**
     * 设置
     * @param id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * 获取
     * @return username
     */
    public String getUsername() {
        return username;
    }

    /**
     * 设置
     * @param username
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * 获取
     * @return password
     */
    public String getPassword() {
        return password;
    }

    /**
     * 设置
     * @param password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    public String toString() {
        return "User{id = " + id + ", username = " + username + ", password = " + password + "}";
    }
}
package com.itheima.test;

import java.util.ArrayList;

public class Test6 {
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<>();
        //创建用户对象
        User user = new User("00001","张","11111");
        User user1 = new User("00002","王","11111");
        User user2 = new User("00003","王","11111");
        //将用于对象添加至集合
        list.add(user);
        list.add(user1);
        list.add(user2);
        //调用方法
        boolean flag = SelectInfoFromUserId(list, "00001");
        //打印结果
        System.out.println(flag);
    }

    //需要信息和id  信息是集合ArrayList<User> list
    public static boolean SelectInfoFromUserId(ArrayList<User> list,String id){
        for (int i = 0; i < list.size(); i++) {
            //i是集合里的用户对象
            //list.get(i).getId().equals(id);
            //先list.get 再getid
            User user = list.get(i);
            String uid= user.getId();
            if(uid.equals(id)){
                return true;
            }
        }
        return false;
    }
}
package com.itheima.test;

import java.util.ArrayList;

public class Test6test3 {
    public static void main(String[] args) {
        //1.定义集合
        ArrayList<User> list = new ArrayList<>();
        //2.定义用户
        User user = new User("0001","zhang","mima1");
        User user1 = new User("0002","wang","mima2");
        User user2 = new User("0003","li","mima3");
        //3.添加
        list.add(user);
        list.add(user1);
        list.add(user2);
        //4.调用
        int index = getIndex(list, "0001");
        System.out.println(index);
    }

    public static int getIndex(ArrayList <User> list,String id) {
        for (int i = 0; i < list.size(); i++) {
            //获取数组中的id
            //list.get(i).getId().equals(id);
            User user = list.get(i);
            //获取
            String uid = user.getId();
            //比较
            if(uid.equals(id)){
                return i;
            }
        }
        return -1;

    }
}

 

标签:返回,String,list,用户,public,User,password,true,id
From: https://www.cnblogs.com/Wang0626/p/17235781.html

相关文章