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

package ArrayList;
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;
    }

    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;
    }
}
package ArrayList;
import java.util.ArrayList;
/*需求:
* 1.main方法中定义一个集合,存入三个用户对象,用户属性为id username password
* 2.要求:定义一个方法,根据id查找对应的用户信息
* 如果存在返回true
* 不存在返回false*/
public class Test6 {
    public static void main(String[] args) {
//1.创建集合
        ArrayList list=new ArrayList<>();
        //2.创建三个用户对象
        User u1=new User("hema001","karry","123456");
        User u2=new User("hema002","jackson","12345678");
        User u3=new User("hema003","roy","123qwer");
//3.把用户对象添加到集合当中
        list.add(u1);
        list.add(u2);
        list.add(u3);
        //4.调用方法查看方法是否存在
   boolean flag= contains(list,"hema010");
        System.out.println(flag);
    }
    public static boolean contains(ArrayList list,String id){
        for (int i = 0; i < list.size(); i++) {
            User u= list.get(i);
          String uid=  u.getId();
          if(uid.equals(id)){
              return  true;
          }
        }
        return  false;
    }
}
package ArrayList;
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;
    }

    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;
    }
}
package ArrayList;
import java.util.ArrayList;
/*需求:
 * 1.main方法中定义一个集合,存入三个用户对象,用户属性为id username password
 * 2.要求:定义一个方法,根据id查找对应的用户信息
 * 如果存在返回索引
 * 不存在返回-1*/
public class Test7 {
    public static void main(String[] args) {
        //1.创建集合
        ArrayList list=new ArrayList<>();
        //2.创建三个用户对象
        User u1=new User("hema001","karry","123456");
        User u2=new User("hema002","jackson","12345678");
        User u3=new User("hema003","roy","123qwer");
//3.把用户对象添加到集合当中
        list.add(u1);
        list.add(u2);
        list.add(u3);
        //4.查找索引
        int index = getIndex(list, "hema001");
        System.out.println(index);
    }
    public static int getIndex(ArrayList list,String id){
        for (int i = 0; i < list.size(); i++) {
         User u=  list.get(i);
        String uid= u.getId();
        if(uid.equals(uid)){
          return i;
        }
        }
        return -1;
    }
}

你可能感兴趣的:(windows)