几道面试题

问:如何在大量数据中找到最大的5个?

思路:new一个5个元素的空数组,里面存的一直都是最大的5个,利用binarySearch(二分法)方法找到每个数据应该插入的位置。

 

 1 public static void main(String[] args) {
 2         int[] arr = new int[5];
 3         Random random = new Random();
 4         for(int i = 0; i < 1000000000; i++) {
 5             int num = random.nextInt();
 6             int index = Arrays.binarySearch(arr, num);
 7             //如果num是最小的,跳过
 8             if(index == 0 || index == -1) continue;
 9             //如果是未出现的数
10             if(index < 0)index = -(index+1);
11             //让index位置之前的数都往前挪一位
12             for(int j = 1; j < index; j++) {
13                 arr[j - 1] = arr[j];
14             }
15             //把num放进去
16             arr[index - 1] = num;
17         }
18         
19         System.out.println(Arrays.toString(arr));
20     }

 

问:如何统计字符串中大写英文字母的个数?

 1 public static void countLetters(String str) {
 2         int[] arr = new int[26];
 3         for(int i = 0; i < str.length(); i++) {
 4             char c = str.charAt(i);
 5             //如果是大写英文字母
 6             if(c >= 'A' && c <= 'Z') {
 7                 //该英文字母的数量加1
 8                 arr[c-'A']++;
 9             }
10         }
11         System.out.println(Arrays.toString(arr));
12     }

 

问:利用生产消费者模型实现异步日志

 1 public class Logger {
 2 
 3     private LinkedBlockingQueue<String> queue;
 4     private PrintWriter out;
 5 
 6     public Logger() {
 7         //给个初始容量
 8         queue = new LinkedBlockingQueue<>(10000);
 9     }
10     
11     public void open() {
12         try {
13             //打开一个文件
14             out = new PrintWriter("log.txt");
15             //开一条线程工作
16             Thread t = new Thread() {
17                 @Override
18                 public void run() {
19                     while(true) {
20                         writeFile();
21                     }
22                 }
23             };
24 //        t.setDaemon(true);
25             t.start();
26         } catch (Exception e) {
27             e.printStackTrace();
28         }
29     }
30     
31     //关闭输出流
32     public void close() {
33         out.close();
34     }
35     
36     //往文件里写日志
37     private void writeFile() {
38         try {
39             //从阻塞队列中取出字符串
40             String str = queue.take();
41             out.println(str);
42             out.flush();
43             System.out.println("写日志:" + str);
44         } catch (InterruptedException e) {
45             e.printStackTrace();
46         }
47     }
48     
49     //往阻塞队列中放日志
50     public void putFile(String log) {
51         try {
52             queue.put(log);
53         } catch (InterruptedException e) {
54             e.printStackTrace();
55         }
56     }
57 }

测试类:

public class TestLogger {

    public static void main(String[] args) {
        Logger logger = new Logger();
        // 开启写日志工作(等待状态)
        logger.open();
        // 塞东西
        for (int i = 0; i < 1000; i++) {
            logger.putFile("I LOVE YOU");
        }

        //流实在是太慢了,我们等一会,等它把日志写进文件再关
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 关流
        logger.close();
    }
}

 

posted @ 2019-07-17 17:55  resuscitate  阅读(141)  评论(0编辑  收藏  举报