美文网首页Android面试经验
Android面试 · 代码题

Android面试 · 代码题

作者: Androidad | 来源:发表于2016-08-16 11:46 被阅读405次

    Android面试70题



    这里整理的都是我在面试过程遇到的笔试和面试题,基本都参照和对比了网上的一些解决方案,修复了一些bug。有些题的其他解决方案在注释中有链接,希望对广大Android战友们有所帮助!

    /**
         * 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。
         * 但是要保证汉字不被截半个,如输入(“我ABC”,4),应该输出“我AB”;
         * 输入(“我ABC汉DEF”,6),应该输出“我ABC”而不是“我ABC”+“汉”的半个
         *
         * 考点:汉字截半时对应字节的ASCII码小于0
         */
    
        public static void main(String[] args) throws Exception {
            String src = "我ABC汉DEF";
            System.out.println(spiltString(src, 4));
            System.out.println(spiltString(src, 6));
        }
    
        private static String spiltString(String src, int len) throws Exception {
            if (src == null || src.equals("")) {
                System.out.println("The source String is null!");
                return null;
            }
            byte[] srcBytes = src.getBytes("GBK");
            if (len > srcBytes.length) {
                len = srcBytes.length;
            }
            if (srcBytes[len] < 0) {
                return new String(srcBytes, 0, --len);
            } else {
                return new String(srcBytes, 0, len);
            }
        }
    

    <br >

    /**
         * 在实际开发工作中,对字符串的处理是最常见的编程任务。
         * 本题目即是要求程序对用户输入的字符串进行处理。具体规则如下:
         * a)把每个单词的首字母变为大写
         * b)把数字与字母之间用下划线字符(_)分开,使得更清晰
         * c)把单词中间有多个空格的调整为1个空格
         * 例如:
         * 用户输入:
         * you and me what    cpp2005program
         * 则程序输出:
         * You And Me What Cpp_2005_program
         * 
         * 相关文章:http://blog.csdn.net/u013091087/article/details/43793149
         */
    
        public static void main(String[] args) {
            System.out.println("please input:");
            Scanner scanner = new Scanner(System.in);
            String s = scanner.nextLine();
            scanner.close();
            String[] ss = s.split("\\s+"); // \s表示空格、\t、\n等空白字符
            for (int i = 0; i < ss.length; i++) {
                String up = (ss[i].charAt(0) + "").toUpperCase(); // 大写
                StringBuffer sb = new StringBuffer(ss[i]);
                ss[i] = sb.replace(0, 1, up).toString(); // 首字母替换为大写
                Matcher m = Pattern.compile("\\d+").matcher(ss[i]);
                int fromIndex = 0;
                while (m.find()) {
                    String num = m.group();
                    int index = ss[i].indexOf(num, fromIndex);
                    StringBuffer sbNum = new StringBuffer(ss[i]);
                    ss[i] = sbNum.replace(index, index + num.length(),
                            "_" + num + "_").toString();
                    fromIndex = index + num.length() + 2;
                    if (ss[i].startsWith("_")) { // 去头"_"
                        ss[i] = ss[i].substring(1);
                    }
                    if (ss[i].endsWith("_")) { // 去尾"_"
                        ss[i] = ss[i].substring(0, ss[i].length() - 1);
                    }
                }
            }
            for (int i = 0; i < ss.length - 1; i++) {
                System.out.print(ss[i] + " ");
            }
            System.out.print(ss[ss.length - 1]);
        }
    

    <br >

    /**
         * 举1-2个排序算法,并使用java代码实现
         *
         * 冒泡排序、插入排序、归并排序、基数排序是稳定的排序算法
         * 选择排序、快速排序、希尔排序、堆排序不是稳定的排序算法 
         * 通常情况下快速排序最快,冒泡最慢
         *
         * http://blog.csdn.net/qy1387/article/details/7752973
         * http://bbs.chinaunix.net/thread-3582599-1-1.html
         */
         
        public static void main(String[] args) {
            int[] array = { 1, 5, 84, 54, 62, 32, 77, 19 };
            selectSort(array);
            bubbleSort(array);
            insertSort(array);
    
            for (int data : array) {
                System.out.print(data + " ");
            }
        }
    
        /**
         * 选择排序
         *
         * 思路:从位置1开始每个数与第0个数比较,如果比第0个小则互换,
         * 一轮完成后位置0的数就是最小的;继续从位置2开始与位置1的数比较...
         */
        public static void selectSort(int[] a) {
            for (int i = 0; i < a.length - 1; i++) {
                for (int j = i + 1; j < a.length; j++) {
                    if (a[j] < a[i]) {
                        swap(a, i, j);
                    }
                }
            }
        }
    
        /**
         * 冒泡排序
         *
         * 思路:自上而下相邻两个数比较,大数往下沉,小数网上冒,
         * 一轮完成后最大的数沉到最底;然后其余的数重复操作
         */
        public static void bubbleSort(int[] a) {
            for (int i = 0; i < a.length - 1; i++) {
                for (int j = 0; j < a.length - 1 - i; j++) {
                    if (a[j] > a[j + 1]) {
                        swap(a, j, j + 1);
                    }
                }
            }
        }
    
        /**
         * 插入排序
         *
         * 思路:假设前面(n-1)[n>=2] 个数已经是排好顺序的,现在要把第n个数插到前面的有序数中,
         * 使得这n个数也是排好顺序的。如此反复循环,直到全部排好顺序
         */
        public static void insertSort(int[] a) {
            for (int i = 1; i < a.length; i++) {
                for (int j = i - 1; j >= 0 && a[j] > a[j + 1]; j--) {
                    swap(a, j, j + 1);
                }
            }
        }
    
        public static void swap(int[] a, int i, int j) {
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    

    <br >

    /**
         * 请编写一个多线程程序,其中一个线程完成对某个对象的int成员变量的增加操作, 
         * 即每次加1,另一个线程完成对该对象的成员变量的减操作,即每次减1;
         * 同时要保证该变量的值不会小于0,不会大于1,该变量的初始值为0
         */
        public class TestThread {
    
            public static void main(String[] args) {
                new TestThread().call(new Operation());
            }
    
            void call(final Operation op) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (true) {
                            op.add();
                        }
                    }
                }).start();
    
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (true) {
                            op.sub();
                        }
                    }
                }).start();
            }
        }
    
        class Operation {
    
            int i = 0;
    
            public synchronized void add() {
                if (i < 1) {
                    i++;
                    System.out.println(i);
                }
            }
    
            public synchronized void sub() {
                if (i > 0) {
                    i--;
                    System.out.println(i);
                }
            }
        }
    

    持续更新...

    相关文章

      网友评论

        本文标题:Android面试 · 代码题

        本文链接:https://www.haomeiwen.com/subject/xbkpsttx.html