J2SE之常用类

String 类

Strng 类举例(1)

String 类常用方法(1)

String 类常用方法(2)

String 类常用方法(3)

练习

import java.util.regex.*;
public class TestString {
    public static void main(String[] args) {
        
        //String s = "AaaaABBBBcc&^%adfsfdCCOOkk99876 _haHA";
        //int lCount = 0, uCount = 0, oCount = 0;
        /*
        for(int i=0; i<s.length(); i++) {
            char c = s.charAt(i);
            if(c >= 'a' && c <= 'z') {
                lCount ++;
            } else if (c >='A' && c <= 'Z') {
                uCount ++;
            } else {
                oCount ++;
            }
        }
        */
        /*
        String sL = "abcdefghijklmnopqrstuvwxyz";
        String sU = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        for(int i=0; i<s.length(); i++) {
            char c = s.charAt(i);
            if(sL.indexOf(c) != -1) {
                lCount ++;
            } else if (sU.indexOf(c) != -1) {
                uCount ++;
            } else {
                oCount ++;
            }
        }
        */
        
        /*
        for(int i=0; i<s.length(); i++) {
            char c = s.charAt(i);
            if(Character.isLowerCase(c)) {
                lCount ++;
            } else if (Character.isUpperCase(c)) {
                uCount ++;
            } else {
                oCount ++;
            }
        }
        
        System.out.println(lCount + " " + uCount + " " + oCount);
        */
        String s = "sunjavahpjavaokjavajjavahahajavajavagoodjava";
        
        String sToFind = "java";
        int count = 0;
        int index = -1;
        
        while((index = s.indexOf(sToFind)) != -1) {
            s = s.substring(index + sToFind.length());    //找到的位置+字符串的长度
            count ++;
        }
        
        System.out.println(count);

    }
}
TestString.java

StringBuffer类

StringBuffer常用方法

 

 

 基本数据类型包装类

 包装类常见方法

 

 练习

public class ArrayParser {
    public static void main(String[] args) {
        double[][] d;
        String s = "1,2;3,4,5;6,7,8";
        String[] sFirst = s.split(";");
        d = new double[sFirst.length][];
        for(int i=0; i<sFirst.length; i++) {
            String[] sSecond = sFirst[i].split(",");
            d[i] = new double[sSecond.length];
            for(int j=0; j<sSecond.length; j++) {
                
                d[i][j] = Double.parseDouble(sSecond[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();
        }
    }
}
ArrayParser.java

 Math类举例

 

File 类

package bjsxt;
import java.io.*;
public class TestFile {
  public static void main(String[] args) {
    String separator = File.separator;
    String filename = "myfile.txt";
    String directory = "mydir1" + separator + "mydir2";
    //String directory = "mydir1/mydir2";
    //String directory = "mydir1\\mydir2";
    File f = new File(directory, filename);
    if (f.exists()) {
      System.out.println("文件名:" + f.getAbsolutePath());
      System.out.println("文件大小:" + f.length());
    } else {
      f.getParentFile().mkdirs();
      try {
        f.createNewFile();
      } catch (IOException e) {
       e.printStackTrace();
      }
    }
  }
}
TestFile.java

import java.io.*;

public class FileList {
    public static void main(String[] args) {
        File f = new File("d:/A");
        System.out.println(f.getName());
        tree(f, 1);
    }
    
    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);
            }
        }
    }
    
}
FileList.java

Java.lang.Enum枚举类型

public class TestEnum {
    public enum MyColor { red, green, blue };
    public enum MyDoorOpener {me, mywife};
    
    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("default");
        }
        System.out.println(m);
    }
}
TestEnum.java

 

posted on 2015-09-03 18:27  gimin  阅读(144)  评论(0)    收藏  举报