java筆試題

最近在找工作,接觸到一些筆試題目,雖然網(wǎng)上也有會各種實現(xiàn),但還是覺得自己寫出來能理解的更深刻一點。

當(dāng)時三道題只有一個小時的時間。平時這部分內(nèi)容練的不多,只完成了一道,剩下兩道題都是后面做的,一共花了2個半小時。。。。。

1.實現(xiàn)兩個線程,使之交替打印1-100;

  public class First {

    static class NumThread implements Runnable {

        private int i = 1;

        @Override
        public void run() {

            while (i <= 100) {

                synchronized (this) {
                    notify();
                    System.out.println(Thread.currentThread().getName() + ":\t" + i++);

                    try {
                        if (i <= 100) {
                            wait();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
            System.out.println("線程結(jié)束:" + Thread.currentThread().getName());
        }
    }

    public static void main(String[] args) {


        NumThread numThread = new NumThread();

        Thread thread1 = new Thread(numThread);
        Thread thread2 = new Thread(numThread);

        thread1.start();
        thread2.start();
    }
}

2.實現(xiàn)函數(shù),給定一個字符串?dāng)?shù)組,求該數(shù)組的連續(xù)非空子集

思想是按游標(biāo)分隔成左右兩段,左段整體和右段每個數(shù)據(jù)進(jìn)行連接

public class ArrayCollected {

    public static void main(String[] args) {

        List<String> list = Arrays.asList("a", "b", "c", "d", "e");
        collected(0, 1, list);

    }

    public static void collected(int cutIndexBegin, int cutIndexEnd, List list) {


        if (cutIndexEnd > list.size()) {
            return;
        }

        List subList = list.subList(cutIndexBegin, cutIndexEnd);
        String subStr = dataFormat(subList);

        if (cutIndexEnd - cutIndexBegin == 1) {

            System.out.println(subStr);
        }

        for (int i = cutIndexEnd; i < list.size(); i++) {
            System.out.println(subStr + list.get(i).toString());
        }

        if (cutIndexEnd == list.size()) {

            cutIndexEnd = ++cutIndexBegin;
        }

        collected(cutIndexBegin, cutIndexEnd + 1, list);
    }

    public static String dataFormat(List list) {

        StringBuffer sb = new StringBuffer();
        list.forEach(e -> sb.append(e));
        return sb.toString();
    }
}

舊版實現(xiàn)

public class Second {

    static String exampleData = "abcdeftgy";

    public static void main(String[] args) {


        char[] chars = exampleData.toCharArray();

        ArrayList<Character> characters = new ArrayList<>();
        for (char c : chars) {
            characters.add(c);
        }

        for (int i = 0; i < characters.size(); i++) {
            joint(0, characters.subList(i, characters.size()));
        }
    }

    public static void joint(int cutIndex, List<Character> characters) {

        if (cutIndex > characters.size()) {
            return;
        }
        if (cutIndex == 0) {

            System.out.println(characters.get(cutIndex));

        } else {

            StringBuffer stringBuffer = new StringBuffer();
            characters.subList(0, cutIndex).stream().forEach(character -> stringBuffer.append(character));

            String leftStr = stringBuffer.toString();

            for (int i = cutIndex; i < characters.size(); i++) {
                System.out.println(leftStr + characters.get(i));
            }
        }
        joint(cutIndex + 1, characters);

    }

}

3.文件系統(tǒng)中按逗號分割保存了1億個正整數(shù)(一行10個數(shù),1000萬行),找出其中最大的100個數(shù)

實測結(jié)果使用ArrayList 38秒;而使用LinkList需要86秒。然而我也沒搞明白為啥有這么大的區(qū)別

public class BigFile {

    static class DataArrayStructure {

        int max;
        List<Integer> list;

        public DataArrayStructure(boolean useArrayList) {

            if (useArrayList) {
                list = new ArrayList<>(128);
            } else {
                list = new LinkedList<>();
            }
        }

        public int getMax() {
            return max;
        }

        public void setMax(int max) {
            this.max = max;
        }

        public List<Integer> getList() {
            return list;
        }

        public void addElement(Integer i) {

            if (i > max) {
                max = i;
            }

            this.getList().add(i);

            if (this.list.size() > 100) {
                removeMin();
//                removeLinkMin();
            }

            if (this.list.size() > 100) {
                throw new RuntimeException("數(shù)組超長");
            }

        }

        public void removeMin() {
            int min = this.list.get(0);
            int index = 0;
            for (int i = 1; i < this.list.size(); i++) {
                int j = this.list.get(i).intValue();
                if (j < min) {
                    min = j;
                    index = i;
                }
            }
            this.list.remove(index);
        }

        public void removeLinkMin() {

            Iterator<Integer> iterator = this.list.iterator();
            Integer min = iterator.next();

            while (iterator.hasNext()) {

                Integer t = iterator.next();
                if (t < min) {
                    min = t;
                }
            }
            this.list.remove(min);
        }

    }


    public static void main(String[] args) throws Exception {


        long startTime = System.currentTimeMillis();

        readData(new File("/Users/zhangfan/logs/data.txt"));
//        writeData();
        System.out.println("花費了:" + (System.currentTimeMillis() - startTime) + "毫秒");
    }


    public static void readData(File file) throws IOException {

        FileInputStream fileInputStream = new FileInputStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

        DataArrayStructure data = new DataArrayStructure(true);
        String str;
        while ((str = bufferedReader.readLine()) != null) {

            String[] strings = str.split(",");
            for (int j = 0; j < strings.length; j++) {
                data.addElement(Integer.valueOf(strings[j]).intValue());
            }
        }

        bufferedReader.close();

        System.out.println(JSONObject.toJSONString(data.getList()));
    }


    public static void writeData() throws IOException {

        File file = new File("/Users/zhangfan/logs/data.txt");
        if (file.exists()) {
            file.createNewFile();
        }
        Random random = new Random();

        FileOutputStream fileOutputStream = new FileOutputStream(file);

        for (int i = 0; i < 10000000; i++) {

            StringBuffer stringBuffer = new StringBuffer();
            for (int j = 0; j < 10; j++) {
                stringBuffer.append(random.nextInt());
                stringBuffer.append(",");
            }
            stringBuffer.deleteCharAt(stringBuffer.length() - 1);
            stringBuffer.append("\n");

            fileOutputStream.write(stringBuffer.toString().getBytes(), 0, stringBuffer.toString().getBytes().length);
        }

        fileOutputStream.close();
    }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Java是一種可以撰寫跨平臺應(yīng)用軟件的面向?qū)ο蟮某绦蛟O(shè)計語言。Java 技術(shù)具有卓越的通用性、高效性、平臺移植性和...
    Java小辰閱讀 1,037評論 0 5
  • 都是一些非常非?;A(chǔ)的題,是我最近參加各大IT公司筆試后靠記憶記下來的,經(jīng)過整理獻(xiàn)給與我一樣參加各大IT校園招聘的...
    Java紅茶閱讀 9,528評論 8 142
  • 前言 面試別人,對我來說是一件新奇事,以前都是別人面試我。 我清楚地知道,我在的地域與公司,難以吸引到中國的一流軟...
    匿蟒閱讀 7,063評論 67 97
  • == 和equal方法的區(qū)別答: 1. 1.基本數(shù)據(jù)類型,也稱原始數(shù)據(jù)類型。byte,short,char,int...
    firststep閱讀 143評論 0 0
  • 近期,某大型企業(yè)的高級java開發(fā)工程師的面試題,現(xiàn)給出試題和答案,供大家參考,如有不對之處,請大家指教。 選擇題...
    和奇谷樸閱讀 9,672評論 3 20

友情鏈接更多精彩內(nèi)容