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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© taoshan 中级黑马   /  2016-5-7 11:23  /  752 人查看  /  14 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

1.集合的迭代
2.IO流拷贝图片
3.多线程卖票
4.统计一行字符串中,每一个字符出现的次数

14 个回复

倒序浏览
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Demo1_HashMap {

        /**
         * @param args
         */
        public static void main(String[] args) {
                HashMap<String, Integer> hm = new HashMap<String,Integer>();
                hm.put("张三", 13);
                hm.put("李四", 14);
                hm.put("王五", 15);
                hm.put("赵六", 16);
                //如何遍历Map集合
                /*
                 * 方法1:获取所有的键,通过键去获取值
                 *
                 * 步骤:        1.获取HashMap所有的键
                 *                 2.通过遍历所有的键
                 *                 3.通过键获取每一个值
                 */
                //1.获取HashMap所有的键
                /*Set<String> s = hm.keySet();
                //2.通过遍历所有的键
                for (String string : s) {
                        //3.通过键获取每一个值
                        Integer i = hm.get(string);
                        System.out.println("Key:" +  string + "\t  Value" + i);
                }*/
                /*获取所有的键,通过迭代器遍历,然后通过键获取值
                 * Set<String> s = hm.keySet();
                Iterator<String> it = s.iterator();
                while(it.hasNext()){
                        String key = it.next();
                        Integer i = hm.get(key);
                        System.out.println("Key:" +  key + "\t  Value" + i);
                }*/
                Set<Map.Entry<String, Integer>> s = hm.entrySet();
                Iterator<Map.Entry<String, Integer>> it = s.iterator();
                while(it.hasNext()){
                        Map.Entry<String, Integer> entry = it.next();
                        String key = entry.getKey();
                        Integer value = entry.getValue();
                        System.out.println("Key:" +  key + "\t  Value" + value);
                       
                }
        }

}
回复 使用道具 举报
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo2_BufferedInputStream {

        /**
         * @param args
         */
        public static void main(String[] args) throws IOException{

                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("1.jpg"));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("2.jpg"));
                int num = 0;
                byte[] arr = new byte[1024 * 8];
                while((num = bis.read(arr)) != -1){
                        bos.write(arr,0,num);
                }
                bis.close();
                bos.close();
        }

}
回复 使用道具 举报
public class Ticket implements Runnable{
        private int ticket = 100;

        @Override
        public void run() {
                        while(ticket > 0){
                                synchronized (this) {
                                try {
                                        System.out.println(Thread.currentThread().getName() + "目前还剩" + --ticket + "张票");
                                        Thread.sleep(200);
                                } catch (InterruptedException e) {
                                       
                                        e.printStackTrace();
                                }
                        }
                }
        }
       
       
}
回复 使用道具 举报
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;

public class StringTest1 {

        /**
         * *Test1--取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq",输出格式为:a(2)b(1)k(2)...
         */
        public static void main(String[] args) {
                //调用输入字符串方法
                String str = getString();
                //调用获取字符串出现次数方法
                HashMap<Character, Integer> hm = getStringNum(str);
                //HashMap集合传递给打印字符出现次数方法
                printStrNum(hm);
        }
        /*
         * 统计字符串出现的次数
         */
        public static HashMap<Character, Integer> getStringNum(String str){
                //创建一个HashMap集合对象
                HashMap<Character, Integer> hm = new HashMap<Character,Integer>();
                //将传入的字符串转为字符数组
                char[] arr = str.toCharArray();
                for(char c : arr){
                        //判断集合中是否已经包含了这个键
                        if(hm.containsKey(c)){
                                //如果不包含,传入键为c的字符,值为通过集合获取到指定键的值再加1
                                hm.put(c, hm.get(c) + 1);
                        }else{
                                //否则直接传入c和1;
                                hm.put(c, 1);
                        }
                }
                return hm;
        }
        /*
         * 输入字符串
         */
        public static String getString(){
                //创建键盘录入对象
                Scanner sc = new Scanner(System.in);
                //提示输入一个整数
                System.out.println("请输入字符串");
                //获取键盘录入的字符串
                String str = sc.nextLine();
                //将字符串返回
                return str;
        }
        /*
         * 打印字符串出现的次数
         */
        public static void printStrNum(HashMap<Character, Integer> hm){
                /*//通过HashMap的ketSet方法获取到所有的键,返回一个Set集合
                Set<Character> set = hm.keySet();
                //遍历Set集合
                for (char cha : set) {
                        //获取到了所有的键的值,通过HashMap对象的get方法获取到所有指定键的值,并通过指定格式输出键值对
                        System.out.print(cha + "(" + hm.get(cha) + ")" + " ");
                }*/
                Set<Map.Entry<Character, Integer>> set = hm.entrySet();
                for(Map.Entry<Character, Integer> me : set){
                        System.out.print(me.getKey() + "(" + me.getValue() + ")");
                }
                /*Iterator<Entry<Character, Integer>> it = set.iterator();
                while(it.hasNext()){
                        Entry<Character, Integer> e = it.next();
                        System.out.print(e.getKey() + "(" + e.getValue() + ")");
                }*/
               
        }
}
回复 使用道具 举报
迭代  单列集合没什么  说到迭代无非就想到了迭代器   双列集合  要么就是获取键值对   然后通过getKey和getValue方法获取到就可以了。或者通过键去获取值

IO流就是绕着三个步骤转   读   写  关流

统计字符出现次数  就是把字符串转为字符数组  然后遍历   找个容器存储字符以及字符出现的次数   如果容器里面有了,就取出次数  + 1   如果没有  就将字符以及次数1  进行存储     而这个容器用双列集合正好。。


回复 使用道具 举报
大神啊,顶礼膜拜啊,牛。
回复 使用道具 举报
1、集合的迭代,主要是考察迭代器Iterator的使用。还要注意的是当对象时存储在HashMap和TreeMap中时,要调用keySet()、EntrySet()方法获取到map集合中的键的集合或者键值对的映射关系集合,Set集合中有迭代器,再用迭代器一一遍历。
结合的遍历还有更快捷的方式就是高级For循环。格式:for(类名  变量名:遍历的集合)
2.IO流拷贝图片,主要考察的是IO流的运用,相对较简单。就是自定义两个字节流。需要注意的是Byte数组的大小的确定。
3.多线程卖票,先定义一个Ticket类,定义数量从100递减,系统输出“ticket......”+(num--)。再定义一个售票窗口类,里边定义一个Sell()方法,调用Ticket类的输出方法。要注意的是,售票窗口的该功能引用了共同的资源所以是线程不安全的,需要加线程同步
4.统计一行字符串中,每一个字符出现的次数。就是将该字符串调用toCharArray()转换为字符数组。定义一个TreeMap集合,遍历字符数组,如果该字符不存在与Map集合内,则将这个字符添加进去并添加对应的值为1.如果存在该字符,那么获取该字符现在的对应的值(出现的次数),将该字符添加进去,并把值设为 获取的值+1.

回复 使用道具 举报
wan1137856139 来自手机 中级黑马 2016-5-8 09:53:00
9#
大哥,有没有答案啊
回复 使用道具 举报
多谢分享,怎么没有答案
回复 使用道具 举报
cc19930915 发表于 2016-5-7 21:33
public class Ticket implements Runnable{
        private int ticket = 100;

这个卖票的代码写完了么
回复 使用道具 举报
不会啊     哎 !!!!!!!!
回复 使用道具 举报

  1. public class shouPiao {

  2.   /**
  3.    * 6、 编写三各类Ticket、SealWindow、TicketSealCenter分别代表票信息、售票窗口、
  4.    * 售票中心。
  5.    * 售票中心分配一定数量的票,由若干个售票窗口进行出售,利用你所学的线程知识来模拟
  6.    * 此售票过程。
  7.    *
  8.    * @author 汤柳清
  9.    */
  10.   public static void main(String[] args) {
  11.           shouPiao t = new shouPiao();
  12.     t.new Ticket();

  13.   }

  14.   class Ticket {
  15.     public Ticket() {
  16.       TicketSealCenter tsc = new TicketSealCenter(100);// 定义有100张票
  17.       for (int i = 0; i < 5; i++) {// 定义有5个窗口
  18.         new Thread(new SealWindow(i, tsc)).start();// 启动售票窗口售票
  19.       }
  20.     }
  21.   }

  22.   /**
  23.    * 售票中心类 定义了票的总数,同步售票方法
  24.    */
  25.   class TicketSealCenter {
  26.     int ticketNum = 50;
  27.     boolean flag = false; // 定义票是否卖完

  28.     public TicketSealCenter(int num) {// 定义一个改变票数的方法
  29.       this.ticketNum = num;
  30.     }

  31.     public synchronized void sellTicket(SealWindow s) {
  32.       if (ticketNum > 0) {//票数如果大于0
  33.         int n = s.num + 1;//n表示第几号窗口
  34.         System.out
  35.             .println("第--" + n + "--售票窗口卖出了第" + ticketNum + "张票!");
  36.         ticketNum--;//卖出一张票后减1
  37.       } else {
  38.         flag = true;
  39.       }
  40.     }
  41.   }

  42.   /**
  43.    * 售票窗口类
  44.    */
  45.   class SealWindow implements Runnable {
  46.     int num;//num表示第几号窗口-1,即i
  47.     TicketSealCenter tsc;

  48.     public SealWindow(int num, TicketSealCenter tsc) {
  49.       this.num = num;
  50.       this.tsc = tsc;
  51.     }

  52.     public final void run() {
  53.       while (!tsc.flag) {
  54.         tsc.sellTicket(this); // 调用售票中心类的同步票数
  55.         try {
  56.           Thread.sleep(100);
  57.         } catch (InterruptedException e) {
  58.           e.printStackTrace();
  59.         }
  60.       }
  61.     }
  62.   }
  63. }
复制代码
回复 使用道具 举报
小红花 发表于 2016-5-8 10:37
这个卖票的代码写完了么

搞个测试类,然后创建几个Thread对象  传入Tikcet对象,然后一个一个start不就好了?

题目不就说卖票么,没说卖完了还要补票,所以就这几句代码够了。
回复 使用道具 举报
楼上好多大神啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马