码迷,mamicode.com
首页 > 其他好文 > 详细

J2SE之常用类

时间:2015-09-03 19:10:19      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

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

 

J2SE之常用类

标签:

原文地址:http://www.cnblogs.com/gimin/p/4780487.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!