8月面试的几道编程基础题(持续更新各种面试题)

来源:互联网 发布:知豆电动汽车代理加盟 编辑:程序博客网 时间:2024/06/15 03:32

第一题:面试的过程中,面试官问到了我对一个集合List<Integer> list = new ArrayList<Integer>();先执行如下的操作:

 List<Integer> list = new ArrayList<Integer>();        list.add(1);        list.add(2);        list.add(3);        list.add(4);        list.add(5);        list.add(6);        System.out.println("listSize="+list.size());        list.remove(1);        list.remove(2);        list.remove(3);

输出的结果为多少?有部分新手或者基础偏薄弱的同学看一下立马觉得直接是4 5 6 呗,那你就大错特错了,因为之所以要用Integer左右list元素值,就是为了混淆你的,remove (index) 是移除对应的索引所在的值,并不是直接移除value,而且每次移除后,后面的元素都会向前补一位,索引最后的结果应该是 1 3 5;

. 第二题:就是编写一个统计字符串里面出现的单词个数,例如:
hello 2
good 1
message 3
实现的原理也很简单,利用map集合处理,首先把字符串利用空格进行分割为String[]数组,然后进行遍历字符串,如果每次遍历到相同单词(key),map中value(单词出现的次数)+1,如果是第一次遍历到该单词,直接put到map,value值设置为1即可,最后还要说到Map集合的遍历,首先获取到Set<>集合,Set<Map.Entry<String,Integer>> set = maps.entrySet(); 其次得到遍历的Iterator,Iterator<Map.Entry<String,Integer>> iterator = set.iterator();
利用while(iterator.hasnext())查找遍历下一个元素,

Map.Entry<String,Integer> mms = iterator.next();            String key = mms.getKey();            Integer value = mms.getValue();            System.out.println(key+" "+value);

详细代码我会在后面贴出来的哟。

. 第三题:利用I/O 进行文件的读写,复制单个文件和文件夹内容到指定的目录。
以下代码综合了以上三个面试题,也是为了方便大家看:

import javax.swing.text.html.HTMLDocument;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.util.*;/** * Created by yangtianrui on 17/8/2. */      /*  * 复制单个文件        * @param oldPath String 原文件路径 如:c:/fqf.txt        * @param newPath String 复制后路径 如:f:/fqf.txt        * @return boolean        */public class Copy {    public static void copyFile(String oldPath, String newPath) {        try {            int bytesum = 0;            int byteread = 0;            File oldfile = new File(oldPath);            if (oldfile.exists()) { //文件存在时                InputStream inStream = new FileInputStream(oldPath); //读入原文件                FileOutputStream fs = new FileOutputStream(newPath);                byte[] buffer = new byte[1444];                int length;                while ((byteread = inStream.read(buffer)) != -1) {                    bytesum += byteread; //字节数 文件大小                    System.out.println(bytesum);                    fs.write(buffer, 0, byteread);                }                inStream.close();            }        } catch (Exception e) {            System.out.println("复制单个文件操作出错");            e.printStackTrace();        }    }    /**     * 复制文件夹     * @param oldPath     * @param newPath     */    public static void copyFolder(String oldPath, String newPath) {        try {            (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹            File a = new File(oldPath);            String[] file = a.list(); //  列举出目录下目录或者文件夹的Name            File temp = null;            for (int i = 0; i < file.length; i++) {                if (oldPath.endsWith(File.separator)) {// 判断文件路径是否以"/" 结束                    temp = new File(oldPath + file[i]);                } else {                    temp = new File(oldPath + File.separator + file[i]);                }                if (temp.isFile()) {                    FileInputStream input = new FileInputStream(temp);                    FileOutputStream output = new FileOutputStream(newPath + "/" +                            (temp.getName()).toString());                    byte[] b = new byte[1024 * 5];                    int len;                    while ((len = input.read(b)) != -1) {                        output.write(b, 0, len);                    }                    output.flush();                    output.close();                    input.close();                }                if (temp.isDirectory()) {//如果是子文件夹 递归子文件夹                    copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);                }            }        } catch (Exception e) {            System.out.println("复制整个文件夹内容操作出错");            e.printStackTrace();        }    }    public static void main(String[] args) {          final String FILE_PATHA = "/Users/yangtianrui/Desktop/file";          final String FILE_PATHB = "/Users/yangtianrui/Desktop/file_a";          String str = new String("ytr is a good a student a hello a men  a is new boy");        //copyFile(FILE_PATHA,FILE_PATHB); //  复制单个文件        // copyFolder(FILE_PATHA,FILE_PATHB); //  复制文件夹        init();        CountLetters(str);    }  static void init ()    {        Map<String,String > map = new HashMap<String, String>();        map.put("name","ytr");        map.put("age","23");        map.put("sex","man");        map.put("sex","ddd");        boolean is = map.equals("sex");        String str = map.get("sex");        System.out.println(str);        Set<Map.Entry<String,String>> mm = map.entrySet();        Iterator<Map.Entry<String, String>> iterable = mm.iterator();        while (iterable.hasNext())        {            Map.Entry<String,String> ma = iterable.next();            String key = ma.getKey();            String value = ma.getValue();            System.out.println(key+" "+value);        }        List<Integer> list = new ArrayList<Integer>();        list.add(1);        list.add(2);        list.add(3);        list.add(4);        list.add(5);        list.add(6);        System.out.println("listSize="+list.size());        list.remove(1);        //list.remove(2);        //list.remove(3);        System.out.println("listSize="+list.size());        for (Integer value : list) {            System.out.println(value);        }    }    public static void CountLetters(String str)    {        String [] strs = str.split(" ");        Map<String,Integer> maps = new HashMap<String, Integer>();        for(int i = 0;i<strs.length;i++) {            if (maps.containsKey(strs[i])) {                maps.put(strs[i],maps.get(strs[i])+1);            }else            {                maps.put(strs[i],1);            }        }        Set<Map.Entry<String,Integer>> set = maps.entrySet();        Iterator<Map.Entry<String,Integer>> iterator = set.iterator();        while (iterator.hasNext())        {            Map.Entry<String,Integer> mms = iterator.next();            String key = mms.getKey();            Integer value = mms.getValue();            System.out.println(key+" "+value);        }    }}

第四题:实现对一个数组的倒置:分别使用了两种方法,第一种可能有点投机取巧了,直接把一个数组从尾取出放入一个新的数组,然后输出,另一种方法就是纯属取到leng/2 然后前后交换位置;

 int[] arr = {1,5,9,4,6,7,89,3,99};        int [] revers_arr = new int[arr.length];        int k=0;        for(int i = arr.length-1;i>=0;i--)        {     k++;            for(int j = k-1;j<revers_arr.length;j++)            {                revers_arr[j] = arr[i];                break;            }        }        Reverser(arr);        Sort(arr);        for (int i = 0; i <arr.length ; i++) {            System.out.print(arr[i] +" ");        }    }//  第二种方式    public static void Reverser(int arr[])    {        int leng = arr.length;        for(int i = 0;i<leng/2;i++)        {            int temp = arr[i];            arr[i] = arr[leng-1-i];            arr[leng-1-i] = temp;        }    }
阅读全文
1 0
原创粉丝点击