JAVA进阶20

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("日	一	二	三	四	五	六");
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  ("	");
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) + "*	");
34             }else {
35                 System.out.print(c.get(Calendar.DAY_OF_MONTH) + "	");
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)链表的用法

 1 package cn.zjc.mycollection;
 2 
 3 public class Node {
 4     Node previous;      //上一个节点
 5     Node next;           //下一个节点
 6     Object element;     //元素数据
 7 
 8     public Node(Node previous, Node next, Object element) {
 9         this.previous = previous;
10         this.next = next;
11         this.element = element;
12     }
13 
14     public Node(Object element) {
15         this.element = element;
16     }
17 }
Node
  1 package cn.zjc.mycollection;
  2 
  3 /*
  4  * 自定义一个链表
  5  */
  6 public class SxtLinkedList01 {
  7 
  8     private Node first;
  9     private Node last;
 10 
 11     private int size;
 12 
 13     //插入节点操作
 14     public void add(int index, Object obj) {
 15         Node newNode = new Node(obj);
 16         Node temp = getNode(index);
 17 
 18         if (temp != null) {
 19             Node up = temp.previous;
 20             up.next = newNode;
 21             newNode.previous = up;
 22 
 23             newNode.next = temp;
 24             temp.previous = newNode;
 25         }
 26 
 27     }
 28 
 29     //remove方法
 30     public void remove(int index) {
 31         Node temp = getNode(index);
 32 
 33         if (temp != null) {
 34             Node up = temp.previous;
 35             Node down = temp.next;
 36             if (up != null) {
 37                 up.next = down;
 38             }
 39             if (down != null) {
 40                 down.previous = up;
 41             }
 42             //被删除的元素是第一个元素时
 43             if (index == 0) {
 44                 first = down;
 45             }
 46             //被删除的元素是最后一个时
 47             if (index == size - 1) {
 48                 last = up;
 49             }
 50             size--;
 51         }
 52     }
 53 
 54     //get方法,传递索引,返回对应的内容
 55     public Object get(int index) {
 56         if (index < 0 || index > size - 1) {
 57             throw new RuntimeException("索引数字不合法:" + index);
 58         }
 59 
 60         Node temp = getNode(index);
 61         return temp != null ? temp.element : null;
 62     }
 63 
 64     public Node getNode(int index) {
 65         Node temp = null;
 66 
 67         if (index <= (size >> 1)) {
 68             temp = first;
 69             for (int i = 0; i < index; i++) {
 70                 temp = temp.next;
 71             }
 72         } else {
 73             temp = last;
 74             for (int i = size - 1; i > index; i--) {
 75                 temp = temp.previous;
 76             }
 77         }
 78         return temp;
 79     }
 80 
 81     public void add(Object obj) {
 82         Node node = new Node(obj);
 83 
 84         if (first == null) {
 85 
 86             first = node;
 87             last = node;
 88         } else {
 89             node.previous = last;
 90             node.next = null;
 91 
 92             last.next = node;
 93             last = node;
 94         }
 95         size++;
 96     }
 97 
 98     public String toString() {
 99         Node temp = first;
100         while (temp != null) {
101             System.out.println(temp.element);
102             temp = temp.next;
103         }
104         return "";
105     }
106 
107 
108     public static void main(String[] args) {
109         SxtLinkedList01 list = new SxtLinkedList01();
110 
111         list.add("a");
112         list.add("d");
113         list.add("c");
114         list.add("1");
115         list.add("3");
116         list.add("A");
117 
118         System.out.println(list);
119         System.out.println(list.get(2));
120         System.out.println("==============");
121         list.remove(1);
122         System.out.println(list);
123         System.out.println("==============");
124         list.add(2,"黄飞鸿");
125         System.out.println(list);
126     }
127 }
View Code

 运行结果图

 

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