7-18 常用类下

来源:互联网 发布:android照相机源码 编辑:程序博客网 时间:2024/06/08 16:36

循序渐进

一 StringBuffer

1)StringBuffer代表可变的字符序列
即运算后,此内存区域内容发生改变
2)String代表不可变字符,运算即复制原有值在新出现的内存区域留下结果
3)

public class StringBuff {   public static void main(String[] args) {     String t = "Mis";     char[] a = {'a','b','c'};     StringBuffer s = new StringBuffer(t);     s.append('/').append("IBM").append('/').append("Sun");//append不断添加字符串     System.out.println(s);     StringBuffer s1 = new StringBuffer("数字");     for(int i=0; i<=9; i++) {       s1.append(i);//添加数字       }     System.out.println(s1);     s1.delete(8,s1.length()).insert(0,a);//令s1从第八位截断。留下 数字012345  添加a在第0位     System.out.println(s1);     System.out.println(s1.reverse());//倒序s1   }}


这里写图片描述

public class Doubl {    public static void main(String[] args) {      double[][] d;      String s = "1,2;3,4,5;6,7,8";      String[] sp = s.split(";");      d = new double[sp.length][];      for(int i=0; i<sp.length; i++){        //System.out.println(sp[i]);//验证是否分对        String[] sp1 = sp[i].split(",");        d[i] = new double[sp1.length];        for(int j=0; j<sp1.length; j++) {            d[i][j] = Double.parseDouble(sp1[j]);          System.out.println(sp1[j]);           }//多打印东西有利于调试程序      }      for(int i=0; i<d.length; i++){        for(int j=0; j<d[i].length; j++) {        System.out.print(d[i][j]+" ");         }      }      System.out.println();     }}


编写一个程序,在命令行中以树状结构展现特定的文件夹及其子文件(夹)

 private static void tree(File f, int level) {//递归    String preStr = "";    for(int i=0; i<level; i++){      preStr +="   ";//空格    }    File[] childs = f.listFiles();    for(int i=0; i<childs.length; i++) {      System.out.println(preStr + childs[i].getName());      if(childs[i].isDirectory()) {        tree(childs[i], level +1);      }    }  }    }

四 枚举类型
基本用法

public class TestEnum {      public enum MyColor {red, green, blue};//定义新类型 此类型变量只能取红绿蓝      public static void main(String[] args) {        MyColor m = MyColor.red;        switch(m) {          case red:            System.out.println("red");            break;           case green:             System.out.println("green");             break;           default:             System.out.println(m);        }      }    }

五 包装类

public class BaoZhuang {  public static void main(String[] args) {    Integer i = new Integer(100);//栈空间的i指向堆空间的100    Double d = new Double("123.456");    int j = i.intValue()+d.intValue();//去掉小数点后数字    float f = i.floatValue()+d.floatValue();    System.out.println(j);    System.out.println(f);    double pi = Double.parseDouble("3.1415926");//转换为double型数    double r = Double.valueOf("2.0").doubleValue();//转换为double类型对象 求Double类型数    double s = pi*r*r;    System.out.println(s);    try{         int k = Integer.parseInt("1.25");//字符串无法转为int型    } catch (NumberFormatException e) {        System.out.println("数据格式不对!");      }    System.out.println(Integer.toBinaryString(123)+"B");//以二进制形式表示    System.out.println(Integer.toHexString(123)+"H");//以八进制    System.out.println(Integer.toOctalString(123)+"O");//十六进制  }}