幾道面試題

1.問:如何在大量數據中找到最大的5個?

思路:new一個5個元素的空數組,裏面存的一直都是最大的5個,利用binarySearch(二分法)方法找到每個數據應該插入的位置。

public static void main(String[] args) {
        int[] arr = new int[5];
        Random random = new Random();
        for(int i = 0; i < 1000000000; i++) {
            int num = random.nextInt();
            int index = Arrays.binarySearch(arr, num);
            //如果num是最小的,跳過
            if(index == 0 || index == -1) continue;
            //如果是未出現的數
            if(index < 0)index = -(index+1);
            //讓index位置之前的數都往前挪一位
            for(int j = 1; j < index; j++) {
                arr[j - 1] = arr[j];
            }
            //把num放進去
            arr[index - 1] = num;
        }
        
        System.out.println(Arrays.toString(arr));
    }

2.問:如何統計字符串中大寫英文字母的個數?

public static void countLetters(String str) {
        int[] arr = new int[26];
        for(int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            //如果是大寫英文字母
            if(c >= 'A' && c <= 'Z') {
                //該英文字母的數量加1
                arr[c-'A']++;
            }
        }
        System.out.println(Arrays.toString(arr));
    }

3.問:利用生產消費者模型實現異步日誌

public class Logger {

    private LinkedBlockingQueue<String> queue;
    private PrintWriter out;

    public Logger() {
        //給個初始容量
        queue = new LinkedBlockingQueue<>(10000);
    }
    
    public void open() {
        try {
            //打開一個文件
            out = new PrintWriter("log.txt");
            //開一條線程工作
            Thread t = new Thread() {
                @Override
                public void run() {
                    while(true) {
                        writeFile();
                    }
                }
            };
//        t.setDaemon(true);
            t.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    //關閉輸出流
    public void close() {
        out.close();
    }
    
    //往文件裏寫日誌
    private void writeFile() {
        try {
            //從阻塞隊列中取出字符串
            String str = queue.take();
            out.println(str);
            out.flush();
            System.out.println("寫日誌:" + str);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    //往阻塞隊列中放日誌
    public void putFile(String log) {
        try {
            queue.put(log);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

測試類:

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();
    }
}
發佈了27 篇原創文章 · 獲贊 28 · 訪問量 1986
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章