简简单单就好

最近总是太过浮躁,学习的劲头越来越弱,又太懒,无意间翻出两年前写过的笔记,就贴过来吧。有点乱,面试题会经常遇到吧...

public class ReadLine {

   public static void main(String[] args){


      byte [] buf = new byte[1024];

      String strInfo = null;

      int pos = 0;


      int ch = 0;

      while(true)

      {

         ch = System.in.read();

switch(ch)

{

   case '\r':

      break;

   case '\n':

      strInfo = new String(buf,0,pos);

      if(strInfo.equals("bye"))

      {

         return;

      }

      else

      {

         System.out.println(strInfo);

 pos = 0;

 break;

      }

   default:

      buf[pos] = (byte)ch;

}

      }


   }

}


//integer

public class TestInteger{

   public static void main(String[] args){

      int w = new Integer(args[0]).intValue();

      int h = Integer.parseInt(args[1]);

      for(int i=0;i<h;i++){

         StringBuffer sb = new StringBuffer();

         for(int j=0;j<w;j++){

   sb.append("*");

}

System.out.println(sb.toString());

      }

   }

}


String sb = new String();

for(int j=0;j<w;j++)

{

   sb = sb + "*";

}


//效率高点

StringBuffer sb = new StringBuffer();

for(int j=0;j<w;j++)

{

   sb.append("*");

}



//集合类

Vector Enumeration ArrayList  Collection  Iterator

Set  List


Vector 类与 Enumeration接口


public class TestVector{

   public static void main(String[] args){

      Vector v = new Vector();

      System.out.println("please enter number:");

      while(true)

      {

         int b = System.in.read();

if(b=='\r'||b=='\n')

   break;

else

{

   int num = b - '0';

   v.addElement(new Integer(num));

}

      }


      int sum = 0;

      Enumeration e = v.elements();

      while(e.hasMoreElements())

      {

         Integer intObj = (Integer)e.nextElement();

sum += intObj.intValue();

      }

      System.out.println(sum);

   }

}

//Collection接口与Iterator接口


public class TestCollection{

   public static void main(String[] args){

      Collection v = new ArrayList();

      System.out.println("please enter number:");

      while(true)

      {

         int b = System.in.read();

if(b=='\r'||b=='\n')

   break;

else

{

   int num = b - '0';

   v.add(new Integer(num));

}

      }


      int sum = 0;

      Iterator e = v.iterator();

      while(e.hasNext())

      {

         Integer intObj = (Integer)e.next();

sum += intObj.intValue();

      }

      System.out.println(sum);

   }

}


//Collection Set List的区别如下:

Collection是通用接口

Collection各元素对象之间没有指定的顺序,允许有重复的和

多个null元素对象


Set各元素对象之间没有指定的顺序,不允许有重复元素,最多

允许有一个null元素对象

List各元素对象对象之间有指定的顺序,允许有重复元素和多个null

元素对象



public class TestSort{

   public static void main(String[] args){

      ArrayList a1 = new ArrayList();

      al.add(new Integer(1));

      al.add(new Integer(3));

      al.add(new Integer(2));

      System.out.println(al.toString());


      Collections.sort(al);

      System.out.println(al.toString());

   }

}



//Hashtable类

Hashtable不仅可以像Vector一样动态存储一系列的

对象,而且对存储的每一个对象(称为值)都要安排

另一个对象(称为关键字)与之相关联。


Hashtable numbers = new Hashtable();

numbers.put("one",new Integer(1));

numbers.put("two",new Integer(2));



Integer n = numbers.get("two");

if(n != null)

{

}


用作关键字的类必须覆盖Object.hashCode方法

和Object.equals方法


public class HashtableTest{

   public static void main(){

      Hashtable numbers = new Hashtable();

      numbers.put(new MyKey("zhangsan",18),new Integer(1));

      numbers.put(new MyKey("lisi",15),new Integer(2));

      numbers.put(new MyKey("wangwu",30),new Integer(3));


      Enumration e = numbers.keys();

      while(e.hasMoreElements())

      {

         MyKey key = (MyKey)e.nextElement();

System.out.print(key + "=");


System.out.println(numbers.get(key));

      }


      //numbers.get(new MyKey("zhangsan",18));

   }

}



public class MyKey{

   private String name = null;

   private int age = 0;


   public MyKey(String name,int age)

   {

      this.name = name;

      this.age = age;

   }


   public boolean equals(Object obj){

      if(obj instanceof MyKey)

      {

         MyKey objTemp = (MyKey)obj;

if(name.equals(objTemp.name)&&age ==objTemp.age)

{

   return true;

}

else

{

   return false;

}

      }

      else

      {

         return false;

      }

   }

   public int hashCode(){

      return name.hashCode() + age;

   }


   public String toString(){

      return name + age;

   }

}


//Properties类

Properties类是Hashtable的子类

Properties.store();


//打印程序运行次数

public class PropertiesFile{

   public static void main(String[] args){

      Properties settings = new Properties();

      try{

         settings.load(new FileInputStream("count.txt"));

      }

      catch(Exception e)

      {

         settings.setProperty("count",String.valueOf(0));

      }

      //settings.get("count");

      int c = Integer.parseInt(settings.getProperty("count")) + 1;

      System.out.println("这是第" + c + "次运行");


      //settings.put("count",new Integer(c).toString());

      settings.setProperty("count",new Integer(c).toString());

      try{

         settings.store(new FileOutputStream("count.txt"),"Program is used:");

      }

      catch(Exception e)

      {

         e.printStackTrace();

      }

   }

}



//System   Runtime类

System类

-exit方法  终止虚拟机运行

-currentTimeMillis方法

-Java虚拟机的系统属性

-getProperties  setProperties

System.currentTimeMillis();

//打印虚拟机的系统属性

public class TestProperties{

   public static void main(String[] args){


      Properties sp = System.getProperties();

      Enumeration e = sp.propertyNames();

      while (e.hasMoreElements())

      {

         String key = (String)e.nextElement();

         System.out.println(key + "=" + sp.getProperty(key));

      }


      Process p = null;

      try{

         p = Runtime.getRuntime().exec("notepad.exe TestProperties.java");

         Thread.sleep(5000);

         p.destroy();

      }catch(Exception e1)

      {

      }

   }

}


System.setProperties();


Runtime类

-Runtime.getRuntime静态方法


//日期和时间有关的类

最常用的几个类:Date/DateFormat  Calendar


Calendar

 - add()

 - get()

 - set()

 - getInstance()  static

 - GregorianCalendar  child

 public class TestCalendar{

    public static void main(String[] args){

       Calendar cl = Calendar.getInstance();

       System.out.println(cl.get(Calendar.YEAR) + "年" + cl.get(cl.MONTH) + "月");

       

       cl.add(cl.DAY_OF_YEAR,315);//315天后

    }

 }


Date类

java.text.DateFormat  java.text.SimpleDateFormat


SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");

SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");

try{

Date d = sdf1.parse("2003-03-15");

sdf2.format(d)

}catch(Exception e)

{

}


Timer  TimerTask类

schedule方法

schedule(TimerTask task,long delay)

schedule(TimerTask task,Date time)

schedule(TimeTask task,long delay,long period)

schedule(TimerTask task,Date firstTime,long period)


TimerTask类实现了Runnable接口,要执行的任务由它里面是啥

的run方法来完成


 

new Timer().schedule(

new TimerTask()

{

  public void run()

  {

     try

     {

Runtime.getRuntime().exec("calc.exe");

     }

     catch(Exception e)

     {

e.printStackTrace();

     }

     //结束任务线程的代码

     //Timer.cancel();

     //TimerTask.cancel();

  }

}

,30000);



class MyTimerTask extend TimerTask

{

private tm = null;

public MyTimerTask(Timer tm)

  this.tm = tm;

}

  public void run()

  {

     try

     {

Runtime.getRuntime().exec("calc.exe");

     }

     catch(Exception e)

     {

e.printStackTrace();

     }

     //结束任务线程的代码

     //Timer.cancel();

     //TimerTask.cancel();

  }

}

Timer tm = new Timer();


tm.schedule(new MyTimerTask(tm)

,30000);

//new Thread(new MyTimerTask()).start();



Math   Random





//file


import java.io.*;

public class FileTest{

   public static void main(String[] args){

      File f = new File("1.txt");

      if(f.exists())

      {

         f.delete();

      }

      else

      {

         f.createNewFile();

      }

      System.out.println("File name:" + f.getName());

      System.out.println("File path:" + f.getPath());

      System.out.println("File abs path:" + f.getAbsolutePath());

      System.out.println("File Parent:" + f.getParent());


      System.out.println(f.exists()?"exist":"not exist");

   }

}



AWT的基础知识

GUI


java.awt.Component   java.awt.Container


GUI部分由AWT线程管理



public static void main(String[] args){

   Frame f = new Frame("www.");

   f.add(new Button("ok"));

   f.setSize(300,300);

   f.setVisable();

   try{

      Thread.sleep(5000);

   }

   catch(Exception e){}

   f.dispose();

   

}



AWT事件处理


MouseEvent

WindowEvent

ActionEvent


public class MyWindowListener implements WindowListener{


public void windowClosing(WindowEvent parm1)

{

   parm1.getWindow().setVisible(false);

   //parm1.getSource();

   java.awt.Component parm1.getComponent();


   

   getEngine Engine

   getDongFengEngine DongFengEngine;

}

   public static void main(String[] args)

   {

      

   }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章