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

时间: 2024-07-29 01:47:04

J2SE之常用类的相关文章

J2SE基础:8.系统常用类二

1:基础数据与封装类型之间的转型 A:基础数据类型--->封装类型(对象类型) Boolean boolean_1 = new Boolean(true); byte ---->Byte short---->Short char---->Character int--->Integer long-->Long float-->Float double-->Double B:封装类型--->基础类型 Integer.intValue--->int

QT开发(二十七)——QT常用类(一)

QT开发(二十七)--QT常用类(一) 一.QString 1.QString简介 QString提供了Unicode编码的字符串,使用隐式共享技术来节省内存和不必要的数据拷贝,不必考虑跨平台的兼容性. QString类成员函数中除了 ascii().latin1().utf8().local8Bit()函数,其他所有的函数都是可重入的. 2.QString成员函数 QString::QString ( const QChar * unicode, int size ) QString::QSt

常用类

常用类 1.Java.util.Locale Java.util.Locale类是区域描述类,用来描述当前区域是哪个区域的. 怎么创建一个区域? //直接用类定义的常量来得到(能得到绝大多数有名的国家的语言和国家名,但朝鲜这样的小国家就只能自己用下面的方法定义了). 1.Locale lo=Locale.CHINA 2.Locale lo=new Locale("朝鲜语","朝鲜")//自己定义个区域的语言和国家名字. 两个常用的方法: 1.获得地区国家: //用当

第七章:常用类

第七章:常用类 包装类 java中有8中基本类型,对应有八种包装类作用:包装类中封装了该类型常用的属性和方法,以方便操作.byte---->Byteshort--->Shortint--->Integerlong---->Longfloat---->Floatdouble---->Doublechar---->Characterboolean---->Boolean装箱:将基本数据类型转换成包装类,经常通过构造方法完成.Integer i = new Int

异常处理、常用类

Exception Thowable分为Error.Exception RuntimeException 非检查异常,可以不用强制捕获 捕获异常 try{ 可能出现异常的代码 } catch(Exception e1){ 捕获块 } catch(Exception e2){ 捕获块 } catch(Exception e3){ 捕获块 } ......... catch(Exception en){ 捕获块 } finally{ 不管如何都会执行,一般放入资源关闭. } throws关键字 抛出

java基础教程-常用类(四)

四.常用类 4.1字符串相关的类(String StringBuffer) 4.1.1String类   4.1.2StringBuffer类(代表可变的) 解释一下可变和不可变 String s1 = “hello”; String s2 = “world”; s1+=s2; 实际上又开辟了一块内存,将hello和world copy进去,s1指向新的内存 而StringBuffer只是在s1后面增加了一块内存,把world加上,不需要copy String与StringBuffer只有这一定

2015_1_28_IO与常用类

/*日志名格式  年_月_日_内容 如2015_1_28_IO与常用类*/ ************************************************************************************************************************************************************** 日期:2015年1月 28日 主题:IO .常用类 相关文件夹:oracle/相关课件/第12章_Java

J2SE基础:1.类和对象基础

什么是对象 在Java语言,所有的人,事物或者模块都是一个对象. 相同的对象具有一些相同的特性. 狗,猫,蛇3个对象(动物的对象) 苹果,梨,桔子3个对象(水果的对象) 什么是类 可以将现实生活中的对象经过抽象 这种抽象数据类型称为类. 动物类(Animal) 水果类(Fruit) 类和对象的关系 类是对象的模板(抽象化表示),对象是类的实例化(具体化的展现) 类的组成结构 Java是纯面向对象(除了8种基本数据类型) 而对象是从类产生的.因此类是组成Java程序最基本也是最核心的 元素. 变量

C++ 常用类 string类

===6.3.2使用string对象=== string word="I love China" *链接字符串* string description=adjective  + " " + word; _Note_: 不能连接两个字符串字面量,下面的语句是错误的 string test= "I have" + "a dream"; ===6.3.3访问字符串中的字符=== *读取字符串* getline(cin, text);