随笔分类 -  Java基础

1
摘要:public class Test { public static void main(String[] args) { MyThread m1 = new MyThread("m1"); MyThread m2 = new MyThread("m2"); m1.start(); m2.start(); } } class MyThread extends Thread { MyThread(String s) { super(s); } public void run() { for(int i = 1; i <= 100; i++) { Sys 阅读全文
posted @ 2012-05-04 23:26 spring3 阅读(215) 评论(0) 推荐(0)
摘要:public class Test { public static void main(String[] args) { MyThread myThread = new MyThread("m1"); myThread.start(); //产生分支,子线程开始执行 try{ myThread.join();//------等待合并myThread子线程,主线程才开始执行 } catch(InterruptedException e) {} for(int i = 1; i <= 10; i++) { System.out.println("I... 阅读全文
posted @ 2012-05-04 23:15 spring3 阅读(252) 评论(0) 推荐(0)
摘要:import java.util.*; public class Test { public static void main(String[] args) { MyThread thread = new MyThread(); thread.start(); try { Thread.sleep(10000);//主线程睡眠 } catch(InterruptedException e) { } thread.interrupt(); //-----------不是最好的方法 //thread.flag = false; ... 阅读全文
posted @ 2012-05-04 23:01 spring3 阅读(294) 评论(0) 推荐(0)
摘要://接口------推荐 public class Test { public static void main(String[] args) { Runner1 r = new Runner1(); //r.run();------->不是多线程,只是方法调用 Thread t = new Thread(r); t.start();//必须调用线程类的start()方法 //也可以这样: //new Thread(new Runner1()).start(); for(int i = 0; i < 100; i++) { ... 阅读全文
posted @ 2012-05-04 22:26 spring3 阅读(1073) 评论(0) 推荐(0)
摘要:import java.io.*; // transient 关键字 // serializable 接口 // externalizable 接口 public class Test { public static void main(String[] args) throws Exception{ T t = new T(); t.k = 8; FileOutputStream fos = new FileOutputStream("C:/java/testobjectio.txt"); ObjectOutputStream oos = new Obj... 阅读全文
posted @ 2012-05-04 17:31 spring3 阅读(221) 评论(0) 推荐(0)
摘要:import java.util.*; import java.io.*; //简单的日志功能 public class Test { public static void main(String[] args) { String s = null; BufferedReader br = new BufferedReader ( new InputStreamReader(System.in));//标准输入 try { FileWriter fw = new FileWriter("C:/java/logfile.txt",true)... 阅读全文
posted @ 2012-05-03 22:17 spring3 阅读(436) 评论(0) 推荐(0)
摘要:import java.io.*; public class Test { public static void main(String[] args) { // 字节数组(内存)----<---baos-<---====<==dos==<===程序 ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); try { dos.writeDouble(Math.random()); ... 阅读全文
posted @ 2012-05-03 21:01 spring3 阅读(142) 评论(0) 推荐(0)
摘要:import java.io.*; public class Test { public static void main(String[] args) { try { OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("C:/java/char.txt")); osw.write("microsoftibssunapplehp"); System.out.println(osw.getEncoding());//文本编码 osw.close(); //加了tr... 阅读全文
posted @ 2012-05-03 20:24 spring3 阅读(144) 评论(0) 推荐(0)
摘要:import java.util.Scanner; public class MoFang { public static void main(String[] args) { System.out.println("输入行(列)数:"); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[][] a = new int[n][n]; int i = 0; int j = n / 2; //算法精要 for (int k = 1; k <= n * n; k++) { ... 阅读全文
posted @ 2012-05-03 19:52 spring3 阅读(222) 评论(0) 推荐(0)
摘要:import java.io.*; public class Test { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("C:/java/Test.java"); BufferedInputStream bis = new BufferedInputStream(fis); int c = 0; System.out.println(bis.read()); System.out.println(bis.rea... 阅读全文
posted @ 2012-05-02 16:59 spring3 阅读(362) 评论(0) 推荐(0)
摘要:import java.io.*; public class TestFileReader { public static void main(String[] args) { FileReader fr = null; int c = 0; try { fr = new FileReader("C:/java/Test.java"); while((c = fr.read()) != -1) { System.out.print((char)c); } fr.close(); } catch (FileNotFoundExcepti... 阅读全文
posted @ 2012-05-01 22:18 spring3 阅读(171) 评论(0) 推荐(0)
摘要:import java.io.*; public class Test { public static void main(String[] args) { int b = 0; FileInputStream in = null; try { in = new FileInputStream("C:/java/Test.java"); } catch (FileNotFoundException e) { System.out.println("找不到指定文件"); System.exit(-1); } try { ... 阅读全文
posted @ 2012-05-01 21:37 spring3 阅读(234) 评论(0) 推荐(0)
摘要://TestArsgWords.java import java.util.*; public class TestArgsWords { public static void main(String[] args) { Map<String, Integer> m = new HashMap<String, Integer>(); for(int i = 0; i < args.length; i++) { if(!m.containsKey(args[i])) { m.put(args[i], 1); } else { int fr... 阅读全文
posted @ 2012-05-01 15:06 spring3 阅读(332) 评论(0) 推荐(0)
摘要:import java.util.*; public class TestArgsWords { public static void main(String[] args) { Map m = new HashMap(); for(int i = 0; i < args.length; i++) { int freq = ((Integer) m.get(args[i]) == null ? 0 : (Integer) m.get(args[i])); m.put(args[i], (freq == 0 ? 1 : freq + 1)); } ... 阅读全文
posted @ 2012-05-01 14:41 spring3 阅读(253) 评论(0) 推荐(0)
摘要:import java.util.*; public class Test { public static void main(String[] args) { Map m1 = new HashMap(); Map m2 = new HashMap(); m1.put("one", 1); //m1.put("one",new Integer(1)); m1.put("two", 2); //m1.put("two",new Integer(2)); m1.put("three", 3); / 阅读全文
posted @ 2012-05-01 14:21 spring3 阅读(219) 评论(0) 推荐(0)
摘要:import java.lang.Comparable; import java.util.List; import java.util.LinkedList; import java.util.Collections; public class TestCompareTo { public static void main(String[] args) { List l1 = new LinkedList(); l1.add(new Name("Karl", "M")); l1.add(new Name("Stever", &quo 阅读全文
posted @ 2012-05-01 13:40 spring3 阅读(216) 评论(0) 推荐(0)
摘要:import java.util.*; public class TestCollection { public static void main(String[] args) { List<String> l1 = new LinkedList<String>(); for(int i = 0; i <= 5; i++) { l1.add("a" + i); } System.out.println(l1); l1.add(3, "a100"); System.out.println(l1); l1.set(6, &quo 阅读全文
posted @ 2012-05-01 13:11 spring3 阅读(397) 评论(0) 推荐(0)
摘要:import java.util.ArrayList; import java.util.Collection; public class Test { public static void main(String[] args) { int [] arr = {1, 2, 3, 4, 5}; for(int i : arr) { System.out.print(i + " "); } System.out.println(); Collection<String> c = new ArrayList<String>(); c.add(new St 阅读全文
posted @ 2012-05-01 10:36 spring3 阅读(255) 评论(0) 推荐(0)
摘要:import java.util.Collection; import java.util.HashSet; import java.util.Iterator; public class Test { public static void main(String[] args) { Collection<Name> c = new HashSet<Name>(); c.add(new Name("f1", "l1")); c.add(new Name("f2", "l2")); c.add 阅读全文
posted @ 2012-05-01 10:27 spring3 阅读(191) 评论(0) 推荐(0)
摘要:import java.util.ArrayList; import java.util.Collection; public class Test { public static void main(String[] args) { Collection<Object> c = new ArrayList<Object>(); c.add("hello"); c.add(new Name("f1", "l1")); c.add(new Integer(100)); System.out.println(c.s 阅读全文
posted @ 2012-04-30 20:29 spring3 阅读(282) 评论(0) 推荐(0)

1