Java的基础知识3——常用类

1、String类(不可变的字符序列)

public static String valueOf(…)可以将基本类型数据转换为字符串。
public String[] split(…)可以将一个字符串按照指定的分隔符分隔,返回分隔后的字符数组。

public class Test{

    public static void main(String args[]){

        String s = "we:are:good:friends:!";
        String array[] = s.split(":");
        for(int i=0;i<array.length;i++){
            System.out.print(array[i] + " ");
        }

        int i = 1234567;
        String si = String.valueOf(i);
        int len = si.length();
        System.out.println("\n整数i是" + len + "位数。");
    }
}

查找子串出现的次数

public class Test{

    public static void main(String args[]){

        String s = "java adninjava timan javajavainiajava ()77java";
        String find = "java";
        int counter = 0;
        int index = s.indexOf(find);
        if(index == -1){
            System.out.println("没有找到该字符串。");
            System.exit(0);
        }
        while(index != -1){
            counter++;
            s = s.substring(index + find.length());
            index = s.indexOf(find);
        }
        System.out.println(counter);
    }
}

2、StringBuffer类(可变的字符序列)

public class Test {

    public static void main(String[] args) {

        String s1 = "color";
        StringBuffer sb1 = new StringBuffer(s1);
        sb1.append(':').append("red,").append("green,").append("blue").append('.');
        System.out.println(sb1);

        String s2 = "number:";
        StringBuffer sb2 = new StringBuffer(s2);
        char c[] = {'g','o','o','d',' '};
        for(int i=0;i<=9;i++){
            sb2 = sb2.append(i);
        }
        System.out.println(sb2);
        sb2 = sb2.delete(13, sb2.length());
        System.out.println(sb2);
        sb2 = sb2.insert(7, c);
        System.out.println(sb2);
    }
}

3、基本类型的包装类

编写一个方法,返回一个double型二维数组,数组中的元素通过解析字符串参数获得,如:“1,2;3,4,5;6,7,8”对应的数组:
d[0,0] = 1.0 d[0,1] = 2.0
d[1,0] = 3.0 d[1,1] = 4.0 d[1,2] = 5.0
d[2,0] = 6.0 d[2,1] = 7.0 d[2,2] = 8.0

public class Test {

    public static void main(String[] args) {

        String s = "1,2;3,4,5;6,7,8";
        String sFirst[] = s.split(";");
        double 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]);
                System.out.print(d[i][j] + " ");
            }
            System.out.println();
        }
    }
}

4、Math类

public class Test {

    public static void main(String[] args) {

        double a = Math.random();
        double b = Math.random();
        System.out.println(a);
        System.out.println(b);
        System.out.println(Math.pow(2.0, 3.0));
        System.out.println(Math.exp(3.0));
        System.out.println(Math.log(Math.E));
    }
}

5、File类

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

import java.io.File;

public class Test {

    public static void main(String[] args) {

        File f = new File("E:/a");
        System.out.println(f.getName());
        listFiles(f,1);
    }

    private static void listFiles(File f,int level){
        File files[] = f.listFiles();
        String preString = "";

        for(int j=0;j<level;j++){
            preString  = preString + "    ";
        }

        for(int i=0;i<files.length;i++){
            System.out.println(preString + files[i].getName());
            if(files[i].isDirectory()){
                listFiles(files[i],level+1);
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值