數碼視訊宣講會現場筆試題

數碼視訊宣講會現場筆試題:java

一、list長度爲100,用兩個線程交換取出5個數jsp

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

二、給定日期,得到當月最後一天和上兩個月的第一天ide

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

三、統計某文件夾下包含的jsp,xml,java文件總行數this

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