黑马程序员_集合

------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

一.集合:
 1.数组的弊端:
    1).其长度一旦确定,后期不能更改;
    2).只能存储一种类型的数据;
 2.集合的优点:
    1).对于我们来说,集合是无限大的,我们不用关系长度的问题;
    2).集合可以存储任何的引用数据类型;
 3.集合的层次结构:
    Collection(接口)
       |--List(接口):1.有序;2.能存储重复值;
            |--ArrayList(类):数组实现;不同步,效率高;
            |--Vector(类):数组实现;同步的,效率低;
            |--LinkedList(类):链表实现;不同步,效率高;
  
       |--Set(接口):1.无序的;2.不能存储重复值;

Set集合:
 1.特点:
  1).无序的;
  2).不存储重复值;
 2.HashSet(类):
  1).内部使用:哈希表结构;
  2).验证元素的唯一性:
   hashCode() && equals()
  3).遍历方式:没有特殊方式,使用Collection的方式;
   A.Object数组;
   B.迭代器;
   C.增强for;
 3.LinkedHashSet(类):
  1).内部使用:
   链表:保证有序;
   哈希表:保证唯一;
 4.TreeSet(类):
  1).内部使用:"树"结构;
  2).树结构要对元素进行"排序":
   A.自然排序:
    元素实现Comparable接口;
    重写compareTo()方法;
   B.比较器排序:
    自定义比较器,或者匿名内部类,实现:Comparator接口
    重写compare()方法;
    实例化TreeSet时,传递自定义比较器对象;

Map集合:
 1.特点:
  1).无序;
  2).双列的;键值对存储;
  3).所有的数据结构,都应用于"键",跟值无关;
 2.Map的方法:
  V put(K key,V value)
  V remove(Object key)
  void clear()
  boolean containsKey(Object key)
  boolean containsValue(Object value)
  boolean isEmpty()
  int size()
  获取:
  V get(Object key)
  Set<K> keySet()(可以用于遍历)
  Collection<V> values()
  Set<Map.Entry<K,V>> entrySet()(可用于遍历)
 3.遍历的方式:
  1).keySet()和get(Object key):
  2).entrySet()和Map.Entry的getKey()和getValue():
 4.HashMap(类):
  1).键不能重复,使用hashCode和equals()验证;
 5.LinkedHashMap(类):
  1).有序的;
 6.TreeMap(类):
  1).对"键"进行"排序",使用两种方式:
   A.自然排序;
   B.比较器排序;

下面用MVC模式模拟注册登录案例:

1 public class Demo {
2     public static void main(String[] args) {
3         new UserView().start();
4     }
5 }
 1 public class UserControl {
 2     //控制层,要持有一个持久层的对象
 3     private UserDao userDao = new UserDao();
 4     //注册的方法
 5     public boolean toRegist(UserModel user){
 6         //验证注册名:
 7         /*
 8          * 1.只能包含:小写英文字母,大写英文字母、数字
 9          * 2.长度:6--16位
10          */
11         String regex = "[a-zA-Z0-9]{6,16}";
12         if(!user.getLoginName().matches(regex)){
13             return false;
14         }
15         //验证密码:6位数组
16         regex = "\\d{6}";
17         if(!user.getLoginPwd().matches(regex)){
18             return false;
19         }
20         
21         //验证用户名是否已经存在,交给:持久层;
22         return this.userDao.toRegist(user);
23     }
24     
25     //登录的方法
26     public boolean toLogin(UserModel user){
27         //直接传递给持久层
28         
29         return this.userDao.toLogin(user);
30     }
31     
32 }
 1 import java.util.ArrayList;
 2 
 3 import cn.itcast.user.model.UserModel;
 4 
 5 public class UserDao {
 6     private ArrayList<UserModel> userList = new ArrayList<>();
 7     
 8     //提供一个注册的方法,供前端使用
 9     public boolean toRegist(UserModel user){
10         //验证用户名是否已经存在
11         for(UserModel u : userList){
12             if(user.getLoginName().equals(u.getLoginName())){
13                 return false;
14             }
15         }
16         //存入到集合
17         userList.add(user);
18         return true;
19     }
20     //提供一个登陆的方法,供前端使用
21     public boolean toLogin(UserModel user){
22         //遍历集合,查找相应的用户
23         for(UserModel u : userList){
24             if(user.getLoginName().equals(u.getLoginName()) && 
25                     user.getLoginPwd().equals(u.getLoginPwd())){
26                 return true;//找到
27             }
28         }
29         return false;//没找到
30     }
31 }
 1 public class UserModel {
 2     private String loginName;
 3     private String loginPwd;
 4     public UserModel() {
 5     }
 6     public UserModel(String loginName, String loginPwd) {
 7         this.loginName = loginName;
 8         this.loginPwd = loginPwd;
 9     }
10     public String getLoginName() {
11         return loginName;
12     }
13     public void setLoginName(String loginName) {
14         this.loginName = loginName;
15     }
16     public String getLoginPwd() {
17         return loginPwd;
18     }
19     public void setLoginPwd(String loginPwd) {
20         this.loginPwd = loginPwd;
21     }
22 }
 1 import java.util.Scanner;
 2 
 3 import cn.itcast.user.control.UserControl;
 4 import cn.itcast.user.model.UserModel;
 5 
 6 public class UserView {
 7     private UserControl userControl = new UserControl();
 8     
 9     public void start(){
10         Scanner sc = new Scanner(System.in);
11         //显示菜单:
12         loop:
13         while(true){
14             System.out.print("(1)注册        (2)登录:");
15             int op = sc.nextInt();
16             if(op == 1){//注册
17                 //引导用户输入注册名和密码
18                 System.out.print("请输入注册名:");
19                 String uName = sc.next();
20                 System.out.print("请输入密码:");
21                 String uPwd = sc.next();
22                 //将注册名和密码封装到一个UserModel对象中
23                 UserModel u = new UserModel(uName,uPwd);
24                 //传递给"控制层"
25                 if(!this.userControl.toRegist(u)){
26                     System.out.println("注册失败,可能的原因是:用户名已经存在,或者用户名的长度应该在6-16位之间,并且只能包含大、小写字母和数字,密码只能是6位数字!");
27                     continue loop;
28                 }else{
29                     System.out.println("注册成功!");
30                     continue loop;
31                 }
32             }else if(op == 2){//登录
33                 //引导用户输入
34                 System.out.print("请输入登陆名:");
35                 String uName = sc.next();
36                 System.out.print("请输入密码:");
37                 String uPwd = sc.next();
38                 
39                 //封装UserModel对象,将对象传递给控制层
40                 UserModel user = new UserModel(uName,uPwd);
41                 //传递给控制层;
42                 if(!this.userControl.toLogin(user)){
43                     System.out.println("登陆失败,可能由于用户名或密码错误,请重新登录!");
44                     continue loop;
45                 }else{
46                     System.out.println("登录成功! 欢迎  " + uName + " 进入系统!!");
47                     continue loop;
48                 }
49             }
50             
51         }
52     }
53 }

 

------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

转载于:https://www.cnblogs.com/Erric-Zhou1992/p/4761878.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值