贴一些演示简单API的代码
2010-09-27 21:00
447 查看
有部分使用不正确,不要模仿:
/** * 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 |
相关文章推荐
- 一个使用PJSUA-API编写的简单例子,演示了SIP注册,呼叫,语音交互(少于200行代码)【译】
- OC中KVC设计模式简单实用<代码演示>
- 本地推送 简单代码演示
- Arc 自动内存管理 分类Category 类扩展 Block 代码段 和 Protocol协议的一些简单知识的总结
- python 常见破解代码简单演示(不断增加)
- RandomAccessFile类的简单介绍,以及代码演示
- 百度地图SDK---实现简单定位功能思路解析(采用BaiduMapsAPIDemo代码)
- <kernel>/scripts/checkpatch.pl脚本可用来检查代码书写不规范和作一些简单的代码静态检查
- 关于线程的一些概念和关于线程返回值的简单示例代码
- Javascript 拖拽的一些简单的应用(逐行分析代码,让你轻松了拖拽的原理)
- eoLinker-API_Shop_开发工具类API调用的代码示例合集:六位图片验证码生成、四位图片验证码生成、简单验证码识别等
- 最简单的 DIV + Javascript 分页演示代码
- 通过beego快速创建一个Restful风格API项目及API文档自动化 本文演示如何快速(一分钟内,不写一行代码)的根据数据库及表创建一个Restful风格的API项目,及提供便于在线测试API的界
- vc画图的一些简单方法演示,各种DC
- 展示C代码覆盖率的gcovr工具简单介绍及相关命令使用演示样例
- FastJson一些常见方法(API):处理JSON变得好简单
- 最简单的换肤(模板替换)应用技术为js+css(附加演示代码)
- iOS_一些简单地功能代码
- Javascript 拖拽的一些简单的应用——逐行分析代码,让你轻松了解拖拽的原理
- 一些常用的php简单命令代码集锦