添加用户对象并判定是否存在

打印 上一主题 下一主题

主题 567|帖子 567|积分 1703


  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. /**
  4. * @author gyf
  5. * @ClassName Test
  6. * @Date 2024/8/3 18:02
  7. * @Version V1.0
  8. * @Description :
  9. */
  10. public class Test {
  11.     public static void main(String[] args) {
  12.         Scanner scanner = new Scanner(System.in);
  13.         ArrayList<Student> list = new ArrayList<>();
  14.         Student student1 = new Student("001", "小明", "123");
  15.         Student student2 = new Student("002", "小红", "456");
  16.         Student student3 = new Student("003", "小紫", "789");
  17.         list.add(student1);
  18.         list.add(student2);
  19.         list.add(student3);
  20.         System.out.println("输入你要查询的用户信息id:");
  21.         String id = scanner.next();
  22.         int index = getIndex(list, id);
  23.         boolean flag = search(list,id);
  24.         if (flag) {
  25.             System.out.println("您在查的用户存在,索引为:"+index);
  26.         } else {
  27.             System.out.println("您在查的用户不存在");
  28.         }
  29.     }
  30.     /**
  31.     * @Description:  若存在返回索引,不存在返回-1
  32.     * @Param: [list, id]
  33.     * @return: boolean
  34.     * @Author: gyf
  35.     * @Date: 23:03 2024/8/3
  36.     */
  37.     public static int getIndex(ArrayList<Student> list, String id) {
  38.         for (int i = 0; i < list.size(); i++) {
  39.             String id1 = list.get(i).getId();
  40.             if (id1.equals(id)) {
  41.                 System.out.println(list.get(i).getId()+","+list.get(i).getUsername());
  42.                 return i;
  43.             }
  44.         }
  45.         return -1;
  46.     }
  47.     public static boolean search(ArrayList<Student> list, String id) {
  48.         int id1 = getIndex(list,id);
  49.         if (id1 >=0){
  50.             return true;
  51.         }else {
  52.             return false;
  53.         }
  54.     }
  55. }
复制代码

  1. /**
  2. * @ClassName Student
  3. * @author gyf
  4. * @Date 2024/8/3 21:35
  5. * @Version V1.0
  6. * @Description :
  7. */
  8. public class Student {
  9.     private String id;
  10.     private String username;
  11.     private String password;
  12.     public Student() {
  13.     }
  14.     public Student(String id, String username, String password) {
  15.         this.id = id;
  16.         this.username = username;
  17.         this.password = password;
  18.     }
  19.     public String getId() {
  20.         return id;
  21.     }
  22.     public void setId(String id) {
  23.         this.id = id;
  24.     }
  25.     public String getUsername() {
  26.         return username;
  27.     }
  28.     public void setUsername(String username) {
  29.         this.username = username;
  30.     }
  31.     public String getPassword() {
  32.         return password;
  33.     }
  34.     public void setPassword(String password) {
  35.         this.password = password;
  36.     }
  37. }
复制代码
 


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

刘俊凯

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表