Day 6:Vector类和实现Hashset以及登录窗口的模拟
LinkedList作業:生成撲克牌并且洗牌?
?
import java.util.*;class Poker{String color;String number;public Poker(String color, String number) {super();this.color = color;this.number = number;}@Overridepublic String toString() {return "{"+color+number+"}"; } }public class Demo1 {public static void main(String[] args) {LinkedList pokers = createPokers();shufflePoker(pokers);showPoker(pokers);}private static void showPoker(LinkedList pokers) {for(int i = 0 ; i<pokers.size() ; i++){System.out.print(pokers.get(i));if(i%10==9){System.out.println();}} }private static void shufflePoker(LinkedList pokers) {Random random = new Random();for(int i = 0;i < 100;i++) {int index1 = random.nextInt(pokers.size());int index2 = random.nextInt(pokers.size());//pokers.set(index1, pokers.get(index2));//pokers.set(index2, pokers.get(index1));Poker poker1 = (Poker) pokers.get(index1);Poker poker2 = (Poker) pokers.get(index2);pokers.set(index1, poker2);pokers.set(index2, poker1);} }private static LinkedList createPokers() {String[] colors = {"黑桃","紅桃","梅花","方塊"};String[] numbers = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};LinkedList list = new LinkedList();for(int i = 0 ; i < colors.length ; i++){for(int j = 0 ; j<numbers.length ; j++){list.add(new Poker(colors[i], numbers[j]));}}return list;}}題目:用集合按年齡排序,排序方法(直接排序)
import java.util.*;class Person{String name;int age;public Person(String name, int age) {super();this.name = name;this.age = age;}@Override public String toString() {return "{ 名字("+ this.name+" ),年齡("+ this.age+")}";} }public class Demo2 {public static void main(String[] args) {LinkedList list = new LinkedList();list.add(new Person("李杰", 21));list.add(new Person("李英", 18));list.add(new Person("李漢三", 8)); paixu(list); }private static void paixu(LinkedList list) {for(int i= 0 ; i<list.size() -1 ; i++){for(int j = i+1 ; j<list.size() ; j++){Person p1 = (Person) list.get(i);Person p2 = (Person) list.get(j);if(p1.age>p2.age){list.set(i, p2);list.set(j, p1); }}}System.out.println(list);} }Vector :底層也是維護了一個Object的數組實現的,實現與ArrayList是一樣的,但是Vector是線程安全的,操作效率低。
筆試題: 說出ArrayLsit與Vector的區別?
相同點: ArrayList與Vector底層都是使用了Object數組實現的。
? 不同點:
? ?1. ArrayList是線程不同步的,操作效率高。
?? ?? Vector是線程同步的,操作效率低。
? ?2. ArrayList是JDK1.2出現,Vector是jdk1.0的時候出現的。
?
package cn.itcast.list;import java.util.*; public class Demo1 {public static void main(String[] args) {Vector v = new Vector();v.addElement("李英");v.addElement("李杰");v.addElement("李漢三");Enumeration e = v.elements(); //獲取迭代器while(e.hasMoreElements()){System.out.println(e.nextElement());} } }?Set接口:實現了Set接口的集合類,具備的特點: 無序,不可重復。
因為不可重復的特點所以沒有添加成功。
hashSet的實現原理:
? Haset添加元素的時候,HashSet會先調用元素的hashCode方法得到元素的哈希值 ,
? 然后通過元素的哈希值經過移位等運算,就可以算出該元素在哈希表中的存儲位置。
?算出該元素在哈希表中的存儲位置后
? 情況1: 如果算出元素存儲的位置目前沒有任何元素存儲,那么該元素可以直接存儲到該位置上。
? 情況2: 如果算出該元素的存儲位置目前已經存在有其他的元素了,那么會調用該元素的equals方法與該位置的元素再比較一次
?,如果equals返回的是true,那么該元素與這個位置上的元素就視為重復元素,不允許添加,
如果equals方法返回的是false,那么該元素運行添加(元素能夠在判斷不一致后還可以添加主要是因為哈希表的一個存儲位置存的是一個數組空間,就像二維數組一樣)
?
?
這個代碼中HashCode調用了4次,equals調用一次,因為重寫兩個方法后,只有當HashCode返回值相同時才會調用equals
?
?
登陸程序窗口重做
?
import java.util.ArrayList; import java.util.Collection;public class windows {static Collection <databaseku> collection = new ArrayList();public static void main(String[] args) {new loginWindows();} } import java.awt.*; import javax.swing.*; import java.awt.Event.*; import java.awt.event.ActionListener;public class loginWindows extends JFrame {static JTextField accountjt = new JTextField(10);JPasswordField passwordjt = new JPasswordField(10);JLabel accountjl = new JLabel("用戶名:");JLabel passwordjl = new JLabel("密 碼:");JButton loginjb = new JButton("登陸");JButton signjb = new JButton("注冊");messageListener listener = new messageListener();public loginWindows() {setBounds(100,100,400,250);setVisible(true);setTitle("QQ_Design_by_杰");setResizable(false);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);init();}private void init() {setLayout(null); Container con=getContentPane();accountjl.setBounds(70,15,100,60); accountjt.setBounds(135,30,150,25); passwordjl.setBounds(70,50,100,60); passwordjt.setBounds(135,65,150,25); loginjb.setBounds(100,130,70,35); signjb.setBounds(200,130,70,35); con.add(accountjl); con.add(accountjt); con.add(passwordjl); con.add(passwordjt); con.add(loginjb); con.add(signjb);loginjb.addActionListener(listener);signjb.addActionListener(listener);} } import java.awt.Event.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.*; import javax.swing.*; import javax.swing.JOptionPane;class databaseku {String account; String password; public String getaccount() {return account;}public void setaccount(String account) {this.account = account;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public databaseku(String account, String password) {this.account = account;this.password = password;}@Overridepublic boolean equals(Object obj) {databaseku user = (databaseku)obj;return this.account == user.account;}public String toString() {return "{ 賬號:"+this.account+" 密碼:"+this.password+"}";}public void setAccount(String account) {this.account = account;} }public class messageListener implements ActionListener{public void actionPerformed(ActionEvent e) {// TODO 自動生成的方法存根 databaseku value=null;Iterator it=windows.collection.iterator();String account0 = loginWindows.accountjt.getText();String password0 = loginWindows.accountjt.getText();String buttonName = e.getActionCommand(); if(buttonName.equals("登陸")){while(it.hasNext()) {value=(databaseku)it.next(); if(value.getaccount().equals(account0) && value.getPassword().equals(password0)) {JOptionPane.showMessageDialog(null, "歡迎登陸...");}else{JOptionPane.showMessageDialog(null, "用戶名或者密碼錯誤或者該用戶不存在,登陸失敗...");}}}if(buttonName.equals("注冊")){boolean flag=true;while(it.hasNext()){value=(databaseku)it.next();if(windows.collection.contains(value)){flag=false;break;} }if(flag==false){JOptionPane.showMessageDialog(null, "該賬號已經存在,請重新輸入賬號");}else{windows.collection.add(new databaseku(account0, password0));JOptionPane.showMessageDialog(null, "注冊成功!");JOptionPane.showMessageDialog(null, "當前注冊的人員:"+windows.collection);} }} }?
?
?
?
轉載于:https://www.cnblogs.com/JYDesigner/p/9350331.html
總結
以上是生活随笔為你收集整理的Day 6:Vector类和实现Hashset以及登录窗口的模拟的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 两张图概括struts2执行流程核心(经
- 下一篇: LeaFlet学习之GridLayer扩