zl程序教程

您现在的位置是:首页 >  后端

当前栏目

java自学笔记(1)

JAVA笔记 自学
2023-09-11 14:17:26 时间

  java的ArrayList类型,就是一个数组; 我们可以通过java.util的 iterator进行迭代.

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListIterator {
    public static void main(String[] args){
        ArrayList list = new ArrayList();
        list.add("a");
        list.add("b");
        list.add("c");
        System.out.println("the list adds a,b,c");

        Iterator it = list.iterator();
        System.out.print("iterator results is:");

        while(it.hasNext()){
            System.out.print(it.next() + ",");
        };

        System.out.println();

        list.clear();
        System.out.println("you has been remove list values");
    }
}

 

 

  利用FileOutputStreamObjectOutputStream把object保存到指定的文件中,

  再次利用FileInputStreamObjectInputStream打开我们上次保存的文件,

  这里的new 的Box继承了Serializable;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Box implements Serializable {
    private static final long serialVersionUID = -3450064362986273896L;
    
    private int width;
    private int height;
    
    public static void main(String[] args) {
        Box myBox=new Box();
        myBox.setWidth(50);
        myBox.setHeight(30);
        try {
            FileOutputStream fs=new FileOutputStream("E:\\foo.txt");
            ObjectOutputStream os=new ObjectOutputStream(fs);
            os.writeObject(myBox);
            os.close();
            
            FileInputStream fi=new FileInputStream("E:\\foo.txt");
            ObjectInputStream oi=new ObjectInputStream(fi);
            Box box=(Box)oi.readObject();
            oi.close();
            System.out.println(box.height+","+box.width);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public int getWidth() {
        return width;
    }
    public void setWidth(int width) {
        this.width = width;
    }
    public int getHeight() {
        return height;
    }
    public void setHeight(int height) {
        this.height = height;
    }
}

 

 

  利用java.text.SimpleDateFormat 格式化日期

public class Datetime {

    //会输出当前的时间;
    public static void main(String args[]){
         java.util.Date current=new java.util.Date();
           java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
           String c=sdf.format(current);
           System.out.println(c);
    }

}

 

 

  HashMap和Iterator的使用:

import java.util.HashMap;
import java.util.Iterator;

public class DmHashMap {
    public static void main (String[] args) {
        System.out.println("****HashMap****");

        HashMap hm = new HashMap();
        hm.put("a", "0");
        hm.put("b", "1");
        hm.put("c", "2");
        hm.put("d", "3");

        //相当于js中数组的indexOf方法, 返回的是布尔值;        
        System.out.println( hm.containsKey("a") );

        //相当于js中数组的indexOf方法;
        System.out.println( hm.get("a") );

        //返回一个对象
        System.out.println( hm.entrySet() );

        Iterator it = hm.entrySet().iterator();

        while( it.hasNext() ) {
            System.out.println( it.next() );
        };

        //获取所有的key
        it = hm.keySet().iterator();

        while( it.hasNext() ) {
            System.out.println( it.next() );
        };

        //获取所有的value
        it = hm.values().iterator();

        while( it.hasNext() ) {
            System.out.println( it.next() );
        };
    };
};

 

  循环的顺序是从后面往前面

 

  DecimalFormat是格式化数字以后小数点的工具方法:

import java.text.DecimalFormat;
public class DoubleFormate {
    public static void main(String[] args) {
        java.text.DecimalFormat df=new java.text.DecimalFormat("0.00");
        double d1=123456789.123456;
        double d2=987654321.987654321;

        System.out.println("format1_d1="+df.format(d1));
        System.out.println("format1_d2="+df.format(d2));

        DecimalFormat dff = new DecimalFormat("#,##0.00");
        System.out.println("format2_d1="+dff.format(d1));
        System.out.println("format2_d2="+dff.format(d2));
    }
}

 

  GregorianCalendar是很强大的格式化时间工具方法:

import java.util.*;

public class ShowDate {

    public static void main(String[] args) {
        Calendar calendar = new GregorianCalendar();
        Date trialTime = new Date();
        calendar.setTime(trialTime);

        // PRint out a bunch of interesting things
        System.out.println("ERA: " + calendar.get(Calendar.ERA));
        System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
        System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
        System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
        System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
        System.out.println("DATE: " + calendar.get(Calendar.DATE));
        System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
        System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
        System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
        System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
        System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
        System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
        System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
        System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
        System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
        System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
        System.out.println("ZONE_OFFSET: " + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
        System.out.println("DST_OFFSET: " + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));

        System.out.println("Current Time, with hour reset to 3");
        calendar.clear(Calendar.HOUR_OF_DAY); // so doesn't override
        calendar.set(Calendar.HOUR, 3);
        System.out.println("ERA: " + calendar.get(Calendar.ERA));
        System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
        System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
        System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
        System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
        System.out.println("DATE: " + calendar.get(Calendar.DATE));
        System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
        System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
        System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
        System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
    }
}

 

  使用for循环,循环ArrayList中保存的HashMap数据:

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class sortDemo {
    public static void main(String[] args) {
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        Map<String, String> map1 = new HashMap<String, String>();
        map1.put("FINISH_DATA=2013-03-09", "BEGIN_DATA=2013-03-03");
        Map<String, String> map2 = new HashMap<String, String>();
        map2.put("FINISH_DATA=2013-01-19", "BEGIN_DATA=2013-01-13");
        Map<String, String> map3 = new HashMap<String, String>();
        map3.put("FINISH_DATA=2013-01-12", "BEGIN_DATA=2013-01-06");
        Map<String, String> map4 = new HashMap<String, String>();
        map4.put("FINISH_DATA=2013-01-05", "BEGIN_DATA=2012-12-30");
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        for (Map<String, String> m : list) {
            for (Map.Entry<String, String> en : m.entrySet()) {
                System.out.println(en.getKey() + " , " + en.getValue());
            }
        }
    }
}

 

 

  java里面有三种数组ArrayList, Vector, 以及LinkedList;

import java.util.Date;
import java.util.ArrayList;
import java.util.Vector;
import java.util.Iterator;
import java.util.LinkedList;
 
public class TestIterator{
       public static void main(String[] args) {
              ArrayList a = new ArrayList();
              a.add("China");
              a.add("USA");
              a.add("Korea");
              Iterator  it = a.iterator();
              while(it.hasNext()){
                     String country = (String)it.next();
                     System.out.println(country); 
              };      
             
              Vector v = new Vector();
              v.addElement(new Date());   
              v.addElement(new Date(200008755554L));
              it = v.iterator();
              while(it.hasNext()){
                     Date time = (Date)it.next();
                     System.out.println(time);     
              };     

              LinkedList l = new LinkedList();
              l.add("qihao");
              l.add("nono");
              l.add("hehe");
              l.add("yeah");
              Iterator ite = l.iterator();
              while( ite.hasNext() ){
                     String i = (String) ite.next();
                     System.out.println( i );
              };
       }
}

 

 

  String是不变类,用String修改字符串会新建一个String对象,如果频繁的修改,将会产生很多的String对象,开销很大.因此java提供了一个StringBuffer类,这个类在修改字符串方面的效率比String高了很多。

public class UsingStringBuffer {
    public static void testFindStr() {
        StringBuffer sb = new StringBuffer();
        sb.append("This is a StringBuffer");
        System.out.println("sb.indexOf(\"is\")=" + sb.indexOf("is"));
        System.out.println("sb.indexOf(\"is\")=" + sb.indexOf("is", 3));
        System.out.println("sb.lastIndexOf(\"is\") = " + sb.lastIndexOf("is"));
        System.out.println("sb.lastIndexOf(\"is\", 1) = "
                + sb.lastIndexOf("is", 1));
    }

    public static void main (String[] args) {
        UsingStringBuffer us = new UsingStringBuffer();
        us.testFindStr();
        us.testSubStr();
        us.testCharAtStr();
        us.testAppend();
        us.testDelete();
        us.testInsert();
    }

    public static void testSubStr() {
        StringBuffer sb = new StringBuffer();
        sb.append("This is a StringBuffer");
        System.out.print("sb.substring(4)=" + sb.substring(4));
        System.out.print("sb.substring(4,9)=" + sb.substring(4, 9));
    }

    public static void testCharAtStr() {
        StringBuffer sb = new StringBuffer("This is a StringBuffer");
        System.out.println(sb.charAt(sb.length() - 1));
    }

    public static void testAppend() {
        StringBuffer sb = new StringBuffer("This is a StringBuffer!");
        sb.append(1.23f);
        System.out.println(sb.toString());
    }

    public static void testDelete() {
        StringBuffer sb = new StringBuffer("This is a StringBuffer!");
        sb.delete(0, 5);
        sb.deleteCharAt(sb.length() - 1);
        System.out.println(sb.toString());
    }
        public static void testInsert() {
        StringBuffer sb = new StringBuffer("This is a StringBuffer!");
        sb.insert(2, 'W');
        sb.insert(3, new char[] { 'A', 'B', 'C' });
        sb.insert(8, "abc");
        sb.insert(2, 3);
        sb.insert(3, 2.3f);
        sb.insert(6, 3.75d);
        sb.insert(5, 9843L);
        sb.insert(2, true);
        System.out.println("testInsert: " + sb.toString());
    }

    public static void testReplace() {
        StringBuffer sb = new StringBuffer("This is a StringBuffer!");
        sb.replace(10, sb.length(), "Integer");
        System.out.println("testReplace: " + sb.toString());
    }

    public static void reverseStr() {
        StringBuffer sb = new StringBuffer("This is a StringBuffer!");
        System.out.println(sb.reverse());
    }
}