JAVA进阶21
1、Vector向量
如何选用ArrayList、LinkedList、Vector?
①需要线程安全时,用Vector
②不存在线程安全问题时,并且查找较多用ArrayList(一般使用它)
③不存在线程安全问题时,增加或删除元素较多用LinkedList
2、Map的用法
1 package cn.zjc.mycollection; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 public class TestMap { 7 public static void main(String[] args) { 8 Employee e1 = new Employee(101,"王大",200); 9 Employee e2 = new Employee(102,"王二",201); 10 Employee e3 = new Employee(103,"王仨",202); 11 Employee e4 = new Employee(104,"王四",203); 12 13 Map<Integer,Employee> map = new HashMap<>(); 14 15 map.put(101,e1); 16 map.put(102,e2); 17 map.put(103,e3); 18 map.put(104,e4); 19 Employee emp = map.get(101); 20 System.out.println(emp.getEname()); 21 } 22 } 23 24 //雇员信息 25 class Employee{ 26 private int id; 27 private String ename; 28 private double salary; 29 30 public int getId() { 31 return id; 32 } 33 34 public void setId(int id) { 35 this.id = id; 36 } 37 38 public String getEname() { 39 return ename; 40 } 41 42 public void setEname(String ename) { 43 this.ename = ename; 44 } 45 46 public double getSalary() { 47 return salary; 48 } 49 50 public void setSalary(double salary) { 51 this.salary = salary; 52 } 53 54 public Employee(int id, String ename, double salary) { 55 this.id = id; 56 this.ename = ename; 57 this.salary = salary; 58 59 } 60 }
运行结果图
3、发红包案例
1 package cn.intcast.demo16; 2 3 public abstract class User { 4 private String name; //姓名 5 private int money; //余额,当前用户拥有的钱数 6 7 public User() { 8 } 9 10 public void show(){ 11 System.out.println("我叫:"+name+",我有:"+money+"元"); 12 } 13 public User(String name, int money) { 14 this.name = name; 15 this.money = money; 16 } 17 18 public String getName() { 19 return name; 20 } 21 22 public void setName(String name) { 23 this.name = name; 24 } 25 26 public int getMoney() { 27 return money; 28 } 29 30 public void setMoney(int money) { 31 this.money = money; 32 } 33 }
1 package cn.intcast.demo16; 2 3 import java.util.ArrayList; 4 5 public class Manager extends User { 6 public Manager() { 7 8 } 9 10 public Manager(String name, int money) { 11 super(name, money); 12 } 13 14 public ArrayList<Integer> send(int totalMoney, int count) { 15 //首先需要一个集合,用来存储若干个红包的金额 16 ArrayList<Integer> redList = new ArrayList<>(); 17 18 //确认群主用户有多少余额 19 int leftMoney = super.getMoney(); 20 if (totalMoney > leftMoney) { 21 System.out.println("余额不足!"); 22 return redList; //返回空集合 23 } 24 25 //扣钱,重新设置余额 26 super.setMoney(leftMoney - totalMoney); 27 28 //红包平均拆分成count份 29 int avg = totalMoney / count; 30 //除不尽的余数 31 int mod = totalMoney % count; 32 33 //将红包一个一个放到集合中 34 for (int i = 0; i < count - 1; i++) { 35 redList.add(avg); 36 } 37 //最后一个红包 38 int last = avg + mod; 39 redList.add(last); 40 41 return redList; 42 } 43 }
1 package cn.intcast.demo16; 2 3 import java.util.ArrayList; 4 import java.util.Random; 5 6 public class Member extends User { 7 public Member() { 8 } 9 10 public Member(String name, int money) { 11 super(name, money); 12 } 13 14 public void receive(ArrayList<Integer> list) { 15 //从多个红包中随机抽取一个 16 //随机获取一个集合中的索引编号 17 int index = new Random().nextInt(list.size()); 18 //根据索引,从集合当中删除,并且得到被删除的红包,给我自己 19 int delta = list.remove(index); 20 //当前成员自己本来有多少钱 21 int money = super.getMoney(); 22 //加法,并且重新设置回去 23 super.setMoney(money + delta); 24 } 25 }
1 package cn.intcast.demo16; 2 3 import java.util.ArrayList; 4 5 public class MainRedPacket { 6 public static void main(String[] args) { 7 Manager manager = new Manager("群主",100); 8 Member one = new Member("成员A",10); 9 Member two = new Member("成员B",10); 10 Member three = new Member("成员C",10); 11 12 manager.show(); 13 one.show(); 14 two.show(); 15 three.show(); 16 System.out.println("==============="); 17 18 //群主总共发32,分成3包 19 ArrayList<Integer> redList = manager.send(32,3); 20 //三个成员收红包 21 one.receive(redList); 22 two.receive(redList); 23 three.receive(redList); 24 manager.show(); 25 one.show(); 26 two.show(); 27 three.show(); 28 } 29 }
运行结果图