A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

Collection---迭代的方法:
toArray()
iterator()

迭代器的作用:就是用于抓取集合中的元素。
迭代器的方法:
hasNext()   问是否有元素可遍历。如果有元素可以遍历,返回true,否则返回false 。
  next()    获取元素...        
  remove()  移除迭代器最后一次返回 的元素。

NoSuchElementException 没有元素的异常。
出现的原因: 没有元素可以被迭代了。

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

  要求使用iterator迭代器遍历。

package lx3;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo3 {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                Collection c=new ArrayList();
                c.add("感觉");
                c.add("哈哈");
                c.add("杭电");
                c.add("高考");
                c.add("预科");
               
                Iterator it =c.iterator();
                while(it.hasNext()) {
                        System.out.println("元素:"+ it.next());
                }
                System.out.println("集合的元素:"+ c);
               
                Iterator it1 =c.iterator();
                while(it1.hasNext()) {
                        it1.next();
                        it1.remove();
                }
                System.out.println("集合的元素:"+ c);
        }

}
例题:
使用集合实现注册登陆功能,
第一步: 提示用户选择功能, A(注册)  B(登陆) 。 要求: 功能选择 的时候要忽略大小写。
注册:
1. 提示用户输入注册的账号(数字)与密码,如果输入账号已经存在集合中,提示用户重新输入。 注册完毕之后,把集合中的所有用户信息打印出来。(使用:toArrry()方法)
登陆:
提示用户输入登陆的账号与密码,如果账号与密码这个用户已经存在集合中,那么登陆成功,否则登陆失败。
package lx4;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Scanner;


//用户
class User{
        int id;
        String password;
        public int getId() {
                return id;
        }
        public void setId(int id) {
                this.id = id;
        }
        public String getPassword() {
                return password;
        }
        public void setPassword(String password) {
                this.password = password;
        }
       
        public User(int id,String password) {
                this.id=id;
                this.password=password;
        }
       
        public boolean equals(Object obj) {
                User user=(User)obj;
                return this.id==user.id;
        }
       
        public String toString() {
                return "{账号:"+this.id+"密码:"+this.password+"}";
        }
       
}




public class Demo3 {
    static Scanner scanner=new Scanner(System.in);
        static Collection users=new ArrayList();
               
        public static void main(String[] args) {
                // TODO Auto-generated method stub
        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("请输入账号:");
                int id=scanner.nextInt();
                System.out.println("请输入密码:");
                String password=scanner.next();
               
                boolean isLogin =false;
                Iterator it = users.iterator();
                while(it.hasNext()){
                        User user = (User) it.next();
                        if(user.id==id&&user.password.equals(password)) {
                                isLogin=true;
                        }
                }
                if(isLogin==true) {
                        System.out.println("欢迎登陆..");
                }else {
                        System.out.println("用户名或者密码错误,登陆失败...");
                }
               
        }
        //注册
        public static void reg() {
                User user=null;
                while(true) {
                        System.out.println("请输入账号:");
                        int id =scanner.nextInt();
                        user =new User(id,null);
                        if(users.contains(user)) {
                                System.out.println("该账号已经存在,请重新输入账号");
                        }else {
                                break;
                        }
                }
                System.out.println("请输入密码:");
                String password=scanner.next();
                user.setPassword(password);
                users.add(user);
                System.out.println("注册成功!");
                System.out.println("当前注册的人员:"+users);
               
        }

}

---------------------
【转载】
作者:江南233244
原文:https://blog.csdn.net/qq_3913124 ... 853?utm_source=copy

3 个回复

倒序浏览
奈斯
回复 使用道具 举报
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马