1.需求:定义一个方法,找出int数组中,最大值的索引下标
//定义一个方法 找出int数组中最大值的索引下标 int [] l1 = {23,45,56,67,78545}; Max(l1);//4 int [] l2 = {34,65656,6778,89}; Max(l2);//1 } public static void Max(int [] arr ) { int maxIndex = 0; //最大值的索引的一个变量 for (int i = 1; i < arr.length; i++) { if (arr[i] > arr[maxIndex]) { maxIndex = i; } } System.out.println(maxIndex); }
2.在指定的int数组中找出指定的数据的下标
public static void main(String[] args) { // 在指定的int数组中找出指定的数据的下标 int [] arr = {34,45,56,677,5,78}; int index = indexOf(arr,5); System.out.println(index); } public static int indexOf(int [] arr, int found){ int index = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] == found){ index = i; break; } } return index; }
3.List集合斗地主案例
public class Test1 { public static void main(String[] args) { /** * List集合案例:按照斗地主的规则,完成洗牌发牌的动作。 * 具体规则: * 使用54张牌打乱顺序,三个玩家参与游戏,三人交替摸牌,每人17张,最后三张留作底牌; * 案例分析:准备牌:花色与数字集合组成一张牌。使用Collections类shuffle方法进行随机排序; * 发牌:将牌依次发给三个人,留下底牌三张 * 看牌:直接打印每人手里的牌,和底牌 */ //1.创建一个集合数组当成牌盒子 List<String> Pai = new ArrayList<>(); //2.准备牌 String [] str = {"♥️" , "♠️" ,"♣️" ,"♦️"}; for (int i = 0; i < str.length; i++) { for (int j = 2; j < 11 ; j++) { Pai.add(str[i] + j+ " "); } } // for (String ss:Pai) { // System.out.println(ss); // } String [] str1 = {"A","J","Q","K"}; for (int i = 0; i < str.length; i++) { for (int j = 0; j < str1.length; j++) { Pai.add(str[i] + str1[j]); } } // for (String ss:Pai) { // System.out.println(ss); // } Pai.add("大王"); Pai.add("小王"); //打错顺序 Collections.shuffle(Pai); // for (String ss:Pai) { // System.out.println(ss); // } //3.定义三个玩家 ArrayList<String> play1 = new ArrayList<>(); ArrayList<String> play2 = new ArrayList<>(); ArrayList<String> play3 = new ArrayList<>(); ArrayList<String> diPai = new ArrayList<>(); //4.遍历牌 for (int i = 0; i < Pai.size(); i++) { String card = Pai.get(i); // 留三张底牌 if ( i >= 51){ //余三张存到底牌中 diPai.add(card); }else { //发给三个人 集合 依次发牌 if (i % 3 == 0){ play1.add(card); }else if (i % 3 == 1){ play2.add(card); }else { play3.add(card); } } } //看牌 System.out.println("玩家1"+play1); System.out.println("玩家2"+play2); System.out.println("玩家3"+play3); System.out.println("底牌" + diPai); } }
4.需求:4个景点 80人投票 统计投票人数
public class Test5 { public static void main(String[] args) { //1.把80个学生选择的数据拿进来 String [] selects = {"A","B","C","D"}; StringBuilder sb = new StringBuilder(); Random r = new Random(); for (int i = 0; i <80 ; i++) { sb.append(selects[r.nextInt(selects.length)]); } //2.定义一个Map集合记录最终结果 Map<Character,Integer> infos = new HashMap<>(); //3.遍历80个学生选择的数据 for (int i = 0; i < sb.length(); i++) { //4.提取当前选择景点的字符 char ch = sb.charAt(i); //5.判断Map集合中是否存在这个键 if(infos.containsKey(ch)){ infos.put(ch,infos.get(ch)+1); }else { infos.put(ch,1); } } //4.输出集合 System.out.println(infos); } }
5.生产者消费者模式
public class Test3 { public static void main(String[] args) throws InterruptedException { // 生产消费者模式 Goods goods =new Goods("保时捷", 67.8,true); //true需要被生产 Customer customer =new Customer(goods); Producer producer = new Producer(goods); new Thread(customer).start(); new Thread(producer).start(); } } //1.共享资源对象 两个线程同时操作 class Goods{ private String name;//商品名字 private double price;//商品价格 private boolean shouldProduct;//没有商品为true public Goods() { } @Override public String toString() { return "Goods{" + "name='" + name + '\'' + ", price=" + price + ", shouldProduct=" + shouldProduct + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public boolean isShouldProduct() { return shouldProduct; } public void setShouldProduct(boolean shouldProduct) { this.shouldProduct = shouldProduct; } public Goods(String name, double price, boolean shouldProduct) { this.name = name; this.price = price; this.shouldProduct = shouldProduct; } } //2 .写两个线程 // 消费者线程 class Customer implements Runnable{ //由于两个线程需要共享一个资源 private Goods goods; public Customer(Goods goods){ this.goods = goods; } @Override public void run() { //消费多个 无限制消费 while (true){ // try { // Thread.sleep(3000); // } catch (InterruptedException e) { // e.printStackTrace(); // } synchronized (goods){ // 执行不需要生产的代码 if (!goods.isShouldProduct()){ System.out.println("消费者购买了:" + goods.getName()+ ",价格为:" + goods.getPrice()); //购买完以后商品没了 需要标记为true goods.setShouldProduct(true); //唤醒生产者 goods.notify(); }else{ //需要生产 消费者需要等待 try { // 没有商品 需要生产 消费者进入阻塞状态 goods.wait(); } catch (Exception e) { e.printStackTrace(); } } } } } } // 生产者线程 class Producer implements Runnable{ private Goods goods; public Producer(Goods goods){ this.goods = goods; } @Override public void run() { int count = 0; while (true){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (goods){ if (goods.isShouldProduct()){ if (count %2 ==0 ){ goods.setName("奥迪R9"); goods.setPrice(54.9); }else { goods.setName("五菱"); goods.setPrice(16.8); } // 生产完以后 标记生产状态为false goods.setShouldProduct(false); System.out.println("生产者生产了:" + goods.getName()+",价格为:"+goods.getPrice()); count++; //唤醒消费者 让他去消费 goods.notify(); }else { //有车不需要生产 生产者等待 try { goods.wait(); } catch (Exception e) { e.printStackTrace(); } } } } } }