1、可视化日历程序(简陋版)
package cn.zh.abstrac; import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner; public class TestCalendar {
public static void main(String[] args) throws ParseException {
System.out.println("请输入一个日期:(格式:yyyy-MM-dd)");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = df.parse(str);
Calendar c = new GregorianCalendar();
c.setTime(date); int day = c.get(Calendar.DAY_OF_MONTH);
int days = c.getActualMaximum(Calendar.DATE);
System.out.println("日\t一\t二\t三\t四\t五\t六"); c.set(Calendar.DAY_OF_MONTH, 1); for (int i = 0; i < c.get(Calendar.DAY_OF_WEEK) - 1; i++) {
System.out.print ("\t");
} for (int i = 1; i <= days; i++) {
if (day == c.get(Calendar.DAY_OF_MONTH)){
System.out.print(c.get(Calendar.DAY_OF_MONTH) + "*\t");
}else {
System.out.print(c.get(Calendar.DAY_OF_MONTH) + "\t");
} if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
System.out.println(); //换行
}
c.add(Calendar.DAY_OF_MONTH, 1);
}
}
}
运行结果图
2、使用递归打印目录树结构
package cn.zh.abstrac; import java.io.File; public class TestFile {
public static void main(String[] args) {
File f = new File("E:\\小程序");
printFile(f,0 );
} static void printFile(File file,int level) {
//输出层次
for (int i = 0; i < level; i++) {
System.out.print("-");
}
System.out.println(file.getName());
if (file.isDirectory()) {
File[] files = file.listFiles(); for (File temp : files) {
printFile(temp,level+1);
}
} }
}
运行结果图
3、枚举的用法
package cn.zh.abstrac; import java.util.Random; public class TestEnum {
public static void main(String[] args) {
//枚举遍历
for (week k : week.values()) {
System.out.println(k);
}
//switch语句中使用枚举
int a = new Random().nextInt(4); //生成0,1,2,3的随机数
switch (Season.values()[a]) {
case SPRING:
System.out.println("春天");
break;
case SUMMER:
System.out.println("夏天");
break;
case AUTUMN:
System.out.println("秋天");
break;
case WINTER:
System.out.println("冬天");
break;
}
}
}
enum Season{
SPRING,SUMMER,AUTUMN,WINTER
}
enum week{
星期一,星期二,星期三,星期四,星期五,星期六,星期日,
}
运行结果图
4、异常处理
package cn.zh.abstrac; import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; public class TestA {
public static void main(String[] args) {
FileReader reader = null;
try {
reader = new FileReader("d:/a.txt");
System.out.println("111111111");
char c = (char)reader.read();
char c1 = (char)reader.read();
System.out.println(""+c+c1);
}catch (FileNotFoundException e){
System.out.println("2222222222222222");
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
System.out.println("333333333333333333");
try {
if(reader !=null){
reader.close();
}
}
catch (Exception e){
e.printStackTrace(); }
}
}
}
运行结果图
5、容器和数据结构
(1)简化版-手工实现ArrayList
package cn.zjc.mycollection; public class SxtArrayList {
private Object[] elementDate;
private int size; private static final int DEFALT_CAPACITY = 10; public SxtArrayList() {
elementDate = new Object[DEFALT_CAPACITY];
} public SxtArrayList(int capacity) {
elementDate = new Object[capacity];
} public void add(Object obj) {
elementDate[size++] = obj;
} public String toString() {
StringBuilder sb = new StringBuilder(); sb.append("[");
for (int i = 0; i < size; i++) {
sb.append(elementDate[i]+",");
}
sb.setCharAt(sb.length()-1, ']');
return sb.toString();
} public static void main(String[] args) {
SxtArrayList s1 = new SxtArrayList(20);
s1.add("aa");
s1.add("dd"); System.out.println(s1);
}
}
运行结果图
(2)数组的扩容操作
package cn.zjc.mycollection; public class SxtArray01<E> {
private Object[] elementData;
private int size; private static final int DEFALT_CAPACITY = 10; public SxtArray01() {
elementData = new Object[DEFALT_CAPACITY];
} public SxtArray01(int capacity) {
elementData = new Object[capacity];
} public void add(E element) {
// 什么时候扩容
if (size == elementData.length) {
//怎么扩容(扩容操作)
Object[] newArray = new Object[elementData.length + (elementData.length >> 1)];
System.arraycopy(elementData, 0, newArray, 0, elementData.length);
elementData = newArray;
}
elementData[size++] = element;
} public String toString() {
StringBuilder sb = new StringBuilder(); //[a,b,c]
sb.append("[");
for (int i = 0; i < size; i++) {
sb.append(elementData[i] + ",");
}
sb.setCharAt(sb.length() - 1, ']');
return sb.toString();
} public static void main(String[] args) {
SxtArray01 s1 = new SxtArray01(20); for (int i = 0; i < 25; i++) {
s1.add("zh" + i);
}
System.out.println(s1);
}
}
运行结果图
(3)加get/set,索引不合法等
package cn.zjc.mycollection; public class SxtArray01<E> {
private Object[] elementData;
private int size; private static final int DEFALT_CAPACITY = 10; public SxtArray01() {
elementData = new Object[DEFALT_CAPACITY];
} public SxtArray01(int capacity) {
elementData = new Object[capacity];
} public void add(E element) {
// 什么时候扩容
if (size == elementData.length) {
//怎么扩容(扩容操作)
Object[] newArray = new Object[elementData.length + (elementData.length >> 1)];
System.arraycopy(elementData, 0, newArray, 0, elementData.length);
elementData = newArray;
}
elementData[size++] = element;
} public E get(int index){
checkRange(index);
return (E)elementData[index];
} public void set(E element,int index){
checkRange(index);
elementData[index] = element;
}
public void checkRange(int index){
//索引合法判断[0,size)
if (index<0||index>size-1){
//不合法
throw new RuntimeException("索引不合法!"+index);
}
} public String toString() {
StringBuilder sb = new StringBuilder(); //[a,b,c]
sb.append("[");
for (int i = 0; i < size; i++) {
sb.append(elementData[i] + ",");
}
sb.setCharAt(sb.length() - 1, ']');
return sb.toString();
} public static void main(String[] args) {
SxtArray01 s1 = new SxtArray01(20); for (int i = 0; i < 25; i++) {
s1.add("zh" + i);
}
s1.set("AAAAA",10);
System.out.println(s1);
System.out.println(s1.get(26));
}
}
运行结果图
(4)链表的用法
package cn.zjc.mycollection; public class Node {
Node previous; //上一个节点
Node next; //下一个节点
Object element; //元素数据 public Node(Node previous, Node next, Object element) {
this.previous = previous;
this.next = next;
this.element = element;
} public Node(Object element) {
this.element = element;
}
}
Node
package cn.zjc.mycollection; /*
* 自定义一个链表
*/
public class SxtLinkedList01 { private Node first;
private Node last; private int size; //插入节点操作
public void add(int index, Object obj) {
Node newNode = new Node(obj);
Node temp = getNode(index); if (temp != null) {
Node up = temp.previous;
up.next = newNode;
newNode.previous = up; newNode.next = temp;
temp.previous = newNode;
} } //remove方法
public void remove(int index) {
Node temp = getNode(index); if (temp != null) {
Node up = temp.previous;
Node down = temp.next;
if (up != null) {
up.next = down;
}
if (down != null) {
down.previous = up;
}
//被删除的元素是第一个元素时
if (index == 0) {
first = down;
}
//被删除的元素是最后一个时
if (index == size - 1) {
last = up;
}
size--;
}
} //get方法,传递索引,返回对应的内容
public Object get(int index) {
if (index < 0 || index > size - 1) {
throw new RuntimeException("索引数字不合法:" + index);
} Node temp = getNode(index);
return temp != null ? temp.element : null;
} public Node getNode(int index) {
Node temp = null; if (index <= (size >> 1)) {
temp = first;
for (int i = 0; i < index; i++) {
temp = temp.next;
}
} else {
temp = last;
for (int i = size - 1; i > index; i--) {
temp = temp.previous;
}
}
return temp;
} public void add(Object obj) {
Node node = new Node(obj); if (first == null) { first = node;
last = node;
} else {
node.previous = last;
node.next = null; last.next = node;
last = node;
}
size++;
} public String toString() {
Node temp = first;
while (temp != null) {
System.out.println(temp.element);
temp = temp.next;
}
return "";
} public static void main(String[] args) {
SxtLinkedList01 list = new SxtLinkedList01(); list.add("a");
list.add("d");
list.add("c");
list.add("1");
list.add("3");
list.add("A"); System.out.println(list);
System.out.println(list.get(2));
System.out.println("==============");
list.remove(1);
System.out.println(list);
System.out.println("==============");
list.add(2,"黄飞鸿");
System.out.println(list);
}
}
运行结果图