JAVASE基础(十四)

集合

集合: 集合存储任意对象数据的容器。
集合的特点:

1. 集合可以存储任意类型的对象数据
2. 集合的长度是会发生变化的。

集合体系:
———-| Collection 单列集合类的根接口。
—————-| List 如果实现了List接口的集合类,那么该集合类具备的特点: 有序、 元素可重复.
—————-| Set 如果是实行了Set接口的集合类,那么该集合类具备的特点: 无序,元素不可重复

Colllection要掌握方法:

增加: 
     add(E e) 
    addAll(Collection  c)    把一个集合的元素添加到另外一个集合容器上。

删除: 
    clear()             清除集合的元素
    remove(Object o)     删除指定的元素
    removeAll(Collection  c)    删除两个集合中交集元素
    retainAll(Collection  c)   保留两个集合中的交集元素,其他的元素删除。

查看: 
contains(Object o) 
containsAll(Collection<?> c)    如果此 collection 包含指定 collection 中的所有元素,则返回 true
isEmpty()    如果Collection不包含任何的元素,则返回true,否则返回false.
size()      查看集合中的元素个数

迭代 :    
    迭代 :  遍历集合中的元素。
    toArray() 
    iterator()   获取集合中的迭代器 。  迭代器的作用就是用于抓取集合中的元素。
迭代器常用的方法:
    hasNext()   有没有元素可遍历啊?  
     next()     获取当前游标指向的元素,然后游标向下移动一个单位。
    remove()    移除迭代器最后一次返回的元素。
public class Demo2 {

    public static void main(String[] args) {
        //创建一个集合对象
        Collection c = new ArrayList();  //多态 ...
        //添加元素
        c.add("狗娃");
        c.add("狗剩");
        System.out.println("添加成功吗?"+c.add("铁蛋"));

        //创建一个集合对象
        Collection c2 = new ArrayList();
        c2.add("狗娃");
        c2.add("冰冰");   
        c2.add("美美");

        /*
        c.addAll(c2);  // 把c2中所有元素添加到c集合中。

        删除

        c.clear();  //清除集合的所有元素
        System.out.println("删除成功吗?"+c.remove("范冰冰"));  //    删除指定的元素

        System.out.println("删除成功?"+c.removeAll(c2));  //   删除c集合与c2集合中的交集元素。
         */
        c.retainAll(c2); 

        System.out.println("c集合的元素:"+ c);

    }
}
import java.util.Collection;

class Person{

    int id;

    String name;

    public Person(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "{身份证:"+ this.id+" 姓名:"+ this.name+"}";
    }

    @Override
    public boolean equals(Object obj) {
        Person p  = (Person)obj;
        return this.id == p.id;
    }
}

public class Demo3 {

    public static void main(String[] args) {
        Collection c = new ArrayList();  //接口关系下的多态, 方法都是使用了接口实现类的方法,
        //添加元素
        c.add(new Person(110,"狗娃"));
        c.add(new Person(112,"狗剩"));
        c.add(new Person(119,"铁蛋"));


        //在现实圣湖中只有身份证编号一致,则是同一个人
    /*  Person p = new Person(110,"狗娃"); 
        System.out.println("包含该元素吗?"+ c.contains(p));  //  contains 方法底层是依赖了equals方法进行比较的。


        Collection c2 = new ArrayList();
        c2.add(new Person(110,"狗娃"));
        c2.add(new Person(112,"狗剩"));
        System.out.println("包含集合中的所有元素吗?"+ c.containsAll(c2));

        c.clear();
        c.add(null);
        System.out.println("c集合是空 元素吗?"+ c.isEmpty());
        */

        System.out.println("集合的元素个数:"+ c.size());

        System.out.println("集合的元素:"+ c);

    }

}
public class Demo4 {

    public static void main(String[] args) {
        Collection c  = new ArrayList();
        c.add("王林");
        c.add("马云");
        c.add("赵薇");
        c.add("思聪");

        /*  
        遍历集合元素的方式一:可以使用toArray方法遍历
        Object[] arr = c.toArray();  //把集合中的元素存储到一个Object数组中返回。
        for(int i = 0 ; i < arr.length ; i++){
            System.out.print(arr[i]+",");
        }       
        */

        //获取到了迭代器
         Iterator it = c.iterator();

         /*System.out.println("有元素可遍历吗?"+ it.hasNext());
         System.out.println("元素:"+ it.next());

          while(it.hasNext()){
              System.out.println(it.next());
          }
          it.remove();
           */

         //清空集合的元素
         while(it.hasNext()){
             it.next();
             it.remove();
         }

          System.out.println("集合的元素:"+ c);       
    }
}

需求: 实现注册于登陆功能。

要求:
1. 用户选择功能的时候要忽略大小写。
2. 注册的时候要求用户输入用户名与密码。 把用户名与密码的用户信息保存到集合中。
3. 登陆: 提示用户输入用户名与密码,如果用户名与密码一致匹配上集合中的某个元素,那么登陆成功。 (强制要求使用迭代器去实现)

class User{

    private String userName;

    private String password;

    public User(String userName, String password) {
        super();
        this.userName = userName;
        this.password = password;
    }

    public User() {

    }

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

    @Override
    public String toString() {
        return "{用户名:"+ this.userName+" 密码:"+ this.password+"}";
    }
}


public class Demo1 {

    //创建一个集合对象用于存储用户的数据
    static Collection users = new ArrayList();

    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while(true){        
            System.out.println("请选择功能:  A(注册)   B(登陆)");
            String option = scanner.next(); 
            if("a".equalsIgnoreCase(option) ){
                reg();  //注册功能

            }else if("b".equalsIgnoreCase(option)){

                login();
            }else{
                System.out.println("你的选择有误,请重新输入!");
            }
        }
    }


    //登陆功能
    public static void  login(){
        System.out.println("请输入登陆的用户名:");
        String userName = scanner.next();   //狗娃
        System.out.println("请输入登陆的密码");
        String password = scanner.next();  // 234
        //使用迭代器遍历集合
        Iterator it = users.iterator(); //获取迭代器

        boolean isLogin = false; //默认是没有登陆成功...
        while(it.hasNext()){
            User user  = (User) it.next();  // admin , 123 。    狗娃  234
            if(user.getUserName().equals(userName)&&user.getPassword().equals(password)){
                isLogin = true;
                break;
            }
        }

        if(isLogin){
            System.out.println("登陆成功...");
        }else{
            System.out.println("登陆失败...");
        }


    }




    //注册方法...
    public static void reg() {
        System.out.println("请输入注册的用户名:");
        String userName = scanner.next();
        System.out.println("请输入密码:");
        String password = scanner.next();
        //则应该把这些用户信息用于创建一个用户对象
        User user = new User(userName, password);
        users.add(user);
        System.out.println("注册成功...");
        System.out.println("集合的元素:"+ users);
    }




}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值