有部分使用不正确,不要模仿:
/** * enum */ package com;
/** * @author Administrator * */ public class EnumTest {
enum Week { Mon, Tue, Wed, Thu, Fri, Sat, Sun }
/** * @param args */ public static void main(String[] args) { System.out.println(Week.valueOf("Mon")); // System.out.println(Week.Mon); System.out.println(Week.Sun);
Week[] w = Week.values(); for (Week we : w) { System.out.println(we); }
switch (Week.Sun) { case Mon: System.out.println("1"); break; default: System.out.println("default"); }
System.out.println(Week.Mon.compareTo(Week.Fri));
System.out.println(Coffee.small); System.out.println(Coffee.small.getSize()); Coffee[] coffee = Coffee.values(); for(Coffee c: coffee) { System.out.println(c); System.out.println(c.getSize()); }
System.out.println(Week.valueOf("Mon1")); }
}
enum Coffee { small(8), middle(10), large(12);
private int size;
Coffee(int size) { this.size = size; }
public int getSize() { return size; } } |
Exception in thread "main" java.lang.IllegalArgumentException: No enum const class com.EnumTest$Week.Mon1 at java.lang.Enum.valueOf(Enum.java:196) at com.EnumTest$Week.valueOf(EnumTest.java:1) at com.EnumTest.main(EnumTest.java:48) Mon Mon Sun Mon Tue Wed Thu Fri Sat Sun default -4 small 8 small 8 middle 10 large 12
|
/** * */ package com;
/** * @author Administrator * */ public class ForTest {
public static void main(String[] args) { for (int i = 0, j = 10; i < 10;) { System.out.println(i + j); i++; } }
} |
10 11 12
|
/** * */ package com;
import java.util.ArrayList; import java.util.List;
/** * @author Administrator * */ public class TestWildcards { public static void main(String[] args) { List<Integer> myList = new ArrayList<Integer>(); Bar bar = new Bar(); bar.doInsert(myList); } }
class Bar { void doInsert(List<? extends Object> list) { list.contains(new Dog()); // 说不清楚具体是什么 可添加多种对象而造成危险 比如 Cat Dog等。 list.add(new Object); // compile error } }
class Bar2 { void doInsert(List<? super String> list) { list.contains(new Dog()); // string is a ? 合理 list.add("test"); } } |
import java.util.Comparator; import java.util.PriorityQueue;
/** * */
/** * @author Administrator * */ public class PriorityQueueTest {
static class PQsort implements Comparator<Integer> { // inverse sort public int compare(Integer one, Integer two) { return two - one; // unboxing } }
public static void main(String[] args) { int[] ia = { 1, 5, 3, 7, 6, 9, 8 }; // unordered data PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>(); // use // natural // order
for (int x : ia) // load queue pq1.offer(x); for (int x : ia) // review queue System.out.print(pq1.poll() + " "); System.out.println("");
PQsort pqs = new PQsort(); // get a Comparator PriorityQueue<Integer> pq2 = new PriorityQueue<Integer>(10, pqs); // use // Comparator
for (int x : ia) // load queue pq2.offer(x); System.out.println("size " + pq2.size()); System.out.println("peek " + pq2.peek()); System.out.println("size " + pq2.size()); System.out.println("poll " + pq2.poll()); System.out.println("size " + pq2.size()); for (int x : ia) // review queue System.out.print(pq2.poll() + " "); }
} |
1 3 5 6 7 8 9 size 7 peek 9 size 7 poll 9 size 6 8 7 6 5 3 1 null |
/** * */ package com;
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List;
/** * @author Administrator * */ public class CollectionsTest {
/** * @param args */ public static void main(String[] args) {
List list = new ArrayList(); list.add("a"); list.add("b"); list.add("c"); list.add("a23");
Collections.sort(list); int i = Collections.binarySearch(list, "a23"); System.out.println(list);
PQsort pq = new PQsort(); Collections.sort(list,pq); int j = Collections.binarySearch(list, "a23"); System.out.println(i); System.out.println(j);
int m = Collections.binarySearch(list, "a23", pq); System.out.println(m);
System.out.println(list); }
} class PQsort implements Comparator<String> { // inverse sort public int compare(String one, String two) { return two.compareTo(one); // unboxing //return one.compareTo(two); // unboxing } } |
[a, a23, b, c] 1 -1 2 [c, b, a23, a]
|
/** * */ package file;
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Serializable;
/** * @author Administrator * */ public class FileTest implements Serializable {
public static void main(String[] args) {
String filePath = "d://test";
File file = new File(filePath);
file.mkdir();
File fileName = new File("d://test//t.dat"); try { fileName.createNewFile(); } catch (IOException e) { e.printStackTrace(); } // file.delete();
System.out.println(file.getAbsolutePath());
FileWriter fw = null; try { fw = new FileWriter(fileName); fw.append("this is a test. /n"); fw.append("this is another test. /n"); fw.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { fw.close(); } catch (IOException e) { e.printStackTrace(); } }
try { FileReader fr = new FileReader(fileName); while (fr.ready()) { char[] cbuf = new char[1024]; fr.read(cbuf, 0, 1024); System.out.println(cbuf); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
FileReader fr2 = null; try { fr2 = new FileReader(fileName); BufferedReader br = new BufferedReader(fr2); while (br.ready()) { System.out.println(br.readLine()); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
try { FileInputStream fis = new FileInputStream(fileName); byte[] b = new byte[1023]; int num = 0; while ((num = fis.available()) != 0) { System.out.println("num: " + num); fis.read(b, 0, num); System.out.println(b); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
} } |
d:/test this is a test. this is another test.
this is a test. this is another test. num: 40 [B@f72617
|
/** * */ package com;
/** * @author Administrator * */ import java.util.regex.*;
class Regex2 { public static void main(String[] args) { Pattern p = Pattern.compile("//d?"); Matcher m = p.matcher("23"); boolean b = false;
while (b = m.find()) { System.out.print(m.start() + m.group() + m.end()); }
System.out.println("/n"); Matcher m2 = Pattern.compile("ab").matcher("cabcd"); boolean b2 = false;
while (b2 = m2.find()) { System.out.print(m2.start() + m2.group() + m2.end()); } } } |
02113222
1ab3 |