数码视讯宣讲会现场笔试题

数码视讯宣讲会现场笔试题:

1、list长度为100,用两个线程交换取出5个数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package ren.laughing.test.shumashixun;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 两个线程,交换取出5个元素,直到list为空
 * 
 * @author Laughing_Lz
 * @time 2016年9月29日
 */
class SynList {
    public List list = new ArrayList();
 
    public synchronized void get5Counts1() {
        if (list.size() == 0) {
            System.out.println("list已为空,无法取出!");
        } else {
            System.out.println("执行线程1");
            for (int i = 0; i < 5; i++) {
                System.out.println(list.get(0));
                list.remove(0);
            }
            System.out.println("已取出5个");
            try {
                wait();// wait写在前面
                if (list.size() == 0) {
                    System.out.println("list已为空,无法取出!");
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            notify();
        }
    }
 
    public synchronized void get5Counts2() {
        if (list.size() == 0) {
            System.out.println("list已为空,无法取出!");
        } else {
            System.out.println("执行线程2");
            for (int i = 0; i < 5; i++) {
                System.out.println(list.get(0));
                list.remove(0);
            }
            System.out.println("已取出5个");
            notify();// notify写在前面
            try {
                wait();
                if (list.size() == 0) {
                    System.out.println("list已为空,无法取出!");
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
 
    public void set100Counts() {
        for (int i = 0; i < 100; i++) {
            list.add(i);
        }
    }
}
 
class SynQueue1 implements Runnable {
    private SynList synList;
 
    public SynQueue1(SynList synList) {
        super();
        this.synList = synList;
    }
 
    @Override
    public void run() {
        while (synList.list.size() >= 5) {
            synList.get5Counts1();
        }
    }
}
 
class SynQueue2 implements Runnable {
    private SynList synList;
 
    public SynQueue2(SynList synList) {
        super();
        this.synList = synList;
    }
 
    @Override
    public void run() {
        while (synList.list.size() >= 5) {
            synList.get5Counts2();
        }
    }
}
 
public class SynQueue {
    public static void main(String[] args) {
        SynList synList = new SynList();
        synList.set100Counts();
        new Thread(new SynQueue1(synList)).start();
        new Thread(new SynQueue2(synList)).start();
        ;
    }
}

2、给定日期,获得当月最后一天和上两个月的第一天

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package ren.laughing.test.shumashixun;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
/**
 * 关于获取某月份的第一天和最后一天
 * 
 * @author Laughing_Lz
 * @time 2016年9月29日
 */
public class AboutDate {
    /**
     * 以传入日期起,获取i个月后的第一天
     * 
     * @param strDate
     * @param i
     * @return
     * @throws ParseException
     */
    public String getFirstDayOfMonth(String strDate, int i) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(strDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, i);// 添加i个月份
        cal.set(Calendar.DAY_OF_MONTH, 1);// 设置cal为当前日期的第一天
        String firstDayOfMonth = sdf.format(cal.getTime());
        return firstDayOfMonth;
    }
 
    /**
     * 以传入日期起,获取i个月后的最后一天
     * 
     * @param strDate
     * @param i
     * @return
     * @throws ParseException
     */
    public String getLastDayOfMonth(String strDate, int i) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(strDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, i + 1);// cal月份加1
        cal.set(Calendar.DAY_OF_MONTH, 1);// 设置cal为当前月份的第一天
        cal.add(Calendar.DATE, -1);// cal日期加(-1)天
        String lastDayOfMonth = sdf.format(cal.getTime());
        return lastDayOfMonth;
    }
 
    public static void main(String[] args) throws ParseException {
        String strDate = "2016-09-28";
        String result1 = new AboutDate().getFirstDayOfMonth(strDate, 0);
        System.out.println(result1);
        String result2 = new AboutDate().getLastDayOfMonth(strDate, -2);
        System.out.println(result2);
    }
}

3、统计某文件夹下包含的jsp,xml,java文件总行数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package ren.laughing.test.shumashixun;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 统计某文件夹下包含的jsp,xml,java文件总行数
 * 
 * @author Laughing_Lz
 * @time 2016年9月29日
 */
public class AboutFile {
    public static int count4Java = 0;
    public static int count4Xml = 0;
    public static int count4Jsp = 0;
 
    /**
     * 遍历获得某文件夹下所有文件
     * 
     * @param str
     * @param fileList
     * @return
     */
    public List<String> GetFiles(String str, List<String> fileList) {
        File file = new File(str);
        File[] files = file.listFiles();// 列出所有子文件和路径
        // String[] files = file.list();//列出所有子文件和路径名
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {// 是目录,递归
                // str = str + "\\" + files[i].getName();//拼接子文件夹路径
                fileList = GetFiles(files[i].getAbsolutePath(), fileList);// 传入子文件夹绝对路径名,继续遍历
            } else {
                fileList.add(files[i].getAbsolutePath());// 是文件,将绝对路径名放入list
            }
        }
        return fileList;
    }
 
    /**
     * 统计各类文件的总行数
     * 
     * @param fileList
     */
    public void getCounts(List<String> fileList) {
        for (String file : fileList) {
            try {
                BufferedReader br = new BufferedReader(new FileReader(new File(file)));
                String oneLine = br.readLine();
                while (oneLine != null) {
                    if (file.endsWith(".java")) {
                        count4Java++;
                    } else if (file.endsWith(".xml")) {
                        count4Xml++;
                    } else if (file.endsWith(".jsp")) {
                        count4Jsp++;
                    }
                    oneLine = br.readLine();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
 
        }
        System.out.println("java文件总行数:" + count4Java + "\n" + "jsp文件总行数:" + count4Jsp + "\n" + "xml文件总行数:" + count4Xml);
    }
 
    public static void main(String[] args) {
        List<String> fileList = new ArrayList<String>();
        String str = "G:\\test";
        AboutFile af = new AboutFile();
        af.GetFiles(str, fileList);
        af.getCounts(fileList);
    }
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注