- import java.util.ArrayList;
- import java.util.Scanner;
- /**
- * @author gyf
- * @ClassName Test
- * @Date 2024/8/3 18:02
- * @Version V1.0
- * @Description :
- */
- public class Test {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- ArrayList<Student> list = new ArrayList<>();
- Student student1 = new Student("001", "小明", "123");
- Student student2 = new Student("002", "小红", "456");
- Student student3 = new Student("003", "小紫", "789");
- list.add(student1);
- list.add(student2);
- list.add(student3);
- System.out.println("输入你要查询的用户信息id:");
- String id = scanner.next();
- int index = getIndex(list, id);
- boolean flag = search(list,id);
- if (flag) {
- System.out.println("您在查的用户存在,索引为:"+index);
- } else {
- System.out.println("您在查的用户不存在");
- }
- }
- /**
- * @Description: 若存在返回索引,不存在返回-1
- * @Param: [list, id]
- * @return: boolean
- * @Author: gyf
- * @Date: 23:03 2024/8/3
- */
- public static int getIndex(ArrayList<Student> list, String id) {
- for (int i = 0; i < list.size(); i++) {
- String id1 = list.get(i).getId();
- if (id1.equals(id)) {
- System.out.println(list.get(i).getId()+","+list.get(i).getUsername());
- return i;
- }
- }
- return -1;
- }
- public static boolean search(ArrayList<Student> list, String id) {
- int id1 = getIndex(list,id);
- if (id1 >=0){
- return true;
- }else {
- return false;
- }
- }
- }
复制代码
- /**
- * @ClassName Student
- * @author gyf
- * @Date 2024/8/3 21:35
- * @Version V1.0
- * @Description :
- */
- public class Student {
- private String id;
- private String username;
- private String password;
- public Student() {
- }
- public Student(String id, String username, String password) {
- this.id = id;
- this.username = username;
- this.password = password;
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |