码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA进阶20

时间:2019-04-12 19:30:16      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:ace   data   null   txt   simple   out   opened   cli   可视化   

1、可视化日历程序(简陋版)

技术图片
 1 package cn.zh.abstrac;
 2 
 3 import java.text.DateFormat;
 4 import java.text.ParseException;
 5 import java.text.SimpleDateFormat;
 6 import java.util.Calendar;
 7 import java.util.Date;
 8 import java.util.GregorianCalendar;
 9 import java.util.Scanner;
10 
11 public class TestCalendar {
12     public static void main(String[] args) throws ParseException {
13         System.out.println("请输入一个日期:(格式:yyyy-MM-dd)");
14         Scanner sc = new Scanner(System.in);
15         String str = sc.nextLine();
16         DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
17         Date date = df.parse(str);
18         Calendar c = new GregorianCalendar();
19         c.setTime(date);
20 
21         int day = c.get(Calendar.DAY_OF_MONTH);
22         int days = c.getActualMaximum(Calendar.DATE);
23         System.out.println("日\t一\t二\t三\t四\t五\t六");
24 
25         c.set(Calendar.DAY_OF_MONTH, 1);
26 
27         for (int i = 0; i < c.get(Calendar.DAY_OF_WEEK) - 1; i++) {
28             System.out.print  ("\t");
29         }
30 
31         for (int i = 1; i <= days; i++) {
32             if (day == c.get(Calendar.DAY_OF_MONTH)){
33                 System.out.print(c.get(Calendar.DAY_OF_MONTH) + "*\t");
34             }else {
35                 System.out.print(c.get(Calendar.DAY_OF_MONTH) + "\t");
36             }
37 
38 
39             if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
40                 System.out.println();       //换行
41             }
42             c.add(Calendar.DAY_OF_MONTH, 1);
43         }
44     }
45 }
View Code

运行结果图

技术图片

2、使用递归打印目录树结构

技术图片
 1 package cn.zh.abstrac;
 2 
 3 import java.io.File;
 4 
 5 public class TestFile {
 6     public static void main(String[] args) {
 7         File f = new File("E:\\小程序");
 8         printFile(f,0 );
 9     }
10 
11     static void printFile(File file,int level) {
12         //输出层次
13         for (int i = 0; i < level; i++) {
14             System.out.print("-");
15         }
16         System.out.println(file.getName());
17         if (file.isDirectory()) {
18             File[] files = file.listFiles();
19 
20             for (File temp : files) {
21                 printFile(temp,level+1);
22             }
23         }
24 
25     }
26 }
View Code

运行结果图

技术图片

3、枚举的用法

技术图片
 1 package cn.zh.abstrac;
 2 
 3 import java.util.Random;
 4 
 5 public class TestEnum {
 6     public static void main(String[] args) {
 7         //枚举遍历
 8         for (week k : week.values()) {
 9             System.out.println(k);
10         }
11         //switch语句中使用枚举
12         int a = new Random().nextInt(4);  //生成0,1,2,3的随机数
13         switch (Season.values()[a]) {
14             case SPRING:
15                 System.out.println("春天");
16                 break;
17             case SUMMER:
18                 System.out.println("夏天");
19                 break;
20             case AUTUMN:
21                 System.out.println("秋天");
22                 break;
23             case WINTER:
24                 System.out.println("冬天");
25                 break;
26         }
27     }
28 }
29 enum Season{
30     SPRING,SUMMER,AUTUMN,WINTER
31 }
32 enum week{
33     星期一,星期二,星期三,星期四,星期五,星期六,星期日,
34 }
View Code

运行结果图

技术图片

4、异常处理

技术图片
 1 package cn.zh.abstrac;
 2 
 3 import java.io.FileNotFoundException;
 4 import java.io.FileReader;
 5 import java.io.IOException;
 6 
 7 public class TestA {
 8     public static void main(String[] args) {
 9         FileReader reader = null;
10         try {
11             reader = new FileReader("d:/a.txt");
12             System.out.println("111111111");
13             char c = (char)reader.read();
14             char c1 =  (char)reader.read();
15             System.out.println(""+c+c1);
16         }catch (FileNotFoundException e){
17             System.out.println("2222222222222222");
18             e.printStackTrace();
19         }catch (IOException e){
20             e.printStackTrace();
21         }finally {
22             System.out.println("333333333333333333");
23             try {
24                 if(reader !=null){
25                     reader.close();
26                 }
27             }
28             catch (Exception e){
29                 e.printStackTrace();
30 
31             }
32         }
33     }
34 }
View Code

运行结果图

技术图片

5、容器和数据结构

(1)简化版-手工实现ArrayList

技术图片
 1 package cn.zjc.mycollection;
 2 
 3 public class SxtArrayList {
 4     private Object[] elementDate;
 5     private int size;
 6 
 7     private static final int DEFALT_CAPACITY = 10;
 8 
 9     public SxtArrayList() {
10         elementDate = new Object[DEFALT_CAPACITY];
11     }
12 
13     public SxtArrayList(int capacity) {
14         elementDate = new Object[capacity];
15     }
16 
17     public void add(Object obj) {
18         elementDate[size++] = obj;
19     }
20 
21     public String toString() {
22         StringBuilder sb = new StringBuilder();
23 
24         sb.append("[");
25         for (int i = 0; i < size; i++) {
26             sb.append(elementDate[i]+",");
27         }
28         sb.setCharAt(sb.length()-1, ‘]‘);
29         return sb.toString();
30     }
31 
32     public static void main(String[] args) {
33         SxtArrayList s1 = new SxtArrayList(20);
34         s1.add("aa");
35         s1.add("dd");
36 
37         System.out.println(s1);
38     }
39 }
View Code

 运行结果图

技术图片

(2)数组的扩容操作

技术图片
 1 package cn.zjc.mycollection;
 2 
 3 public class SxtArray01<E> {
 4     private Object[] elementData;
 5     private int size;
 6 
 7     private static final int DEFALT_CAPACITY = 10;
 8 
 9     public SxtArray01() {
10         elementData = new Object[DEFALT_CAPACITY];
11     }
12 
13     public SxtArray01(int capacity) {
14         elementData = new Object[capacity];
15     }
16 
17     public void add(E element) {
18         // 什么时候扩容
19         if (size == elementData.length) {
20             //怎么扩容(扩容操作)
21             Object[] newArray = new Object[elementData.length + (elementData.length >> 1)];
22             System.arraycopy(elementData, 0, newArray, 0, elementData.length);
23             elementData = newArray;
24         }
25         elementData[size++] = element;
26     }
27 
28     public String toString() {
29         StringBuilder sb = new StringBuilder();
30 
31         //[a,b,c]
32         sb.append("[");
33         for (int i = 0; i < size; i++) {
34             sb.append(elementData[i] + ",");
35         }
36         sb.setCharAt(sb.length() - 1, ‘]‘);
37         return sb.toString();
38     }
39 
40     public static void main(String[] args) {
41         SxtArray01 s1 = new SxtArray01(20);
42 
43         for (int i = 0; i < 25; i++) {
44             s1.add("zh" + i);
45         }
46         System.out.println(s1);
47     }
48 }
View Code

 运行结果图

 技术图片

(3)加get/set,索引不合法等

技术图片
 1 package cn.zjc.mycollection;
 2 
 3 public class SxtArray01<E> {
 4     private Object[] elementData;
 5     private int size;
 6 
 7     private static final int DEFALT_CAPACITY = 10;
 8 
 9     public SxtArray01() {
10         elementData = new Object[DEFALT_CAPACITY];
11     }
12 
13     public SxtArray01(int capacity) {
14         elementData = new Object[capacity];
15     }
16 
17     public void add(E element) {
18         // 什么时候扩容
19         if (size == elementData.length) {
20             //怎么扩容(扩容操作)
21             Object[] newArray = new Object[elementData.length + (elementData.length >> 1)];
22             System.arraycopy(elementData, 0, newArray, 0, elementData.length);
23             elementData = newArray;
24         }
25         elementData[size++] = element;
26     }
27 
28     public E get(int index){
29         checkRange(index);
30         return (E)elementData[index];
31     }
32 
33     public void set(E element,int index){
34         checkRange(index);
35         elementData[index] = element;
36     }
37     public void checkRange(int index){
38         //索引合法判断[0,size)
39         if (index<0||index>size-1){
40             //不合法
41             throw new RuntimeException("索引不合法!"+index);
42         }
43     }
44 
45     public String toString() {
46         StringBuilder sb = new StringBuilder();
47 
48         //[a,b,c]
49         sb.append("[");
50         for (int i = 0; i < size; i++) {
51             sb.append(elementData[i] + ",");
52         }
53         sb.setCharAt(sb.length() - 1, ‘]‘);
54         return sb.toString();
55     }
56 
57     public static void main(String[] args) {
58         SxtArray01 s1 = new SxtArray01(20);
59 
60         for (int i = 0; i < 25; i++) {
61             s1.add("zh" + i);
62         }
63         s1.set("AAAAA",10);
64         System.out.println(s1);
65         System.out.println(s1.get(26));
66     }
67 }
View Code

 运行结果图

技术图片

(4)

 

JAVA进阶20

标签:ace   data   null   txt   simple   out   opened   cli   可视化   

原文地址:https://www.cnblogs.com/Anemia-BOY/p/10691520.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!