java.lang包 下

java.lang:java基础语言包
包装类:基本数据类型-------对象
Integer Charater
Integer i =new Integer(12);
valueof(String str):String --------------包装类对象
parseXxx(String):String---->j基本数据类型

 

 

Java中parse()和valueOf(),toString()的区别

 

 

随机生成一个20位的字符串,去除里面重复的字符

import java.util.Random;
public class Demo1 {

    public static void main(String[] args) {
        // 产生随机字符串 a=97 b=98 c=99
        String sourceStr = "";
        String destStr = "";
        Random random = new Random();
        for (int i = 0; i < 20; i++) {
            //ASCII字符集a=97 b=98 c=99
            sourceStr += (char) (random.nextInt(26) + 97);
        }
        System.out.println(sourceStr);
        for (int i = 0; i < sourceStr.length(); i++) {
            char c = sourceStr.charAt(i);
            if (destStr.indexOf(c) == -1) {
                destStr += c;
            } else {
                continue;
            }
        }
        System.out.println(destStr);
    }

}

String特性:不变性
String str="abc";

StringBuffer类用于表示可以修改的字符串

public class StringBufferDemo {

    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        System.out.println(sb.length());
        System.out.println(sb.capacity());
        
        // StringBuffer sb1 = new StringBuffer("abc");
        // System.out.println(sb1.length());
        // System.out.println(sb.capacity());
        //增加,修改,删除,查询
        //增加数据append(追加) insert(插入)
        sb.append("xyz");
        System.out.println(sb.toString());
        sb.append("abc");
        System.out.println(sb.toString());
        sb.insert(3, "xxx");
        System.out.println(sb.toString());
        
        //修改 replace
        sb.replace(2, 5, "oooo");
        System.out.println(sb.toString());
        //setCharAt:修改某个位置的字符
        sb.setCharAt(2, '9');
        System.out.println(sb.toString());
        
        //删除
        sb.delete(2, 5);
        System.out.println(sb.toString());
        sb.deleteCharAt(3);
        System.out.println(sb.toString());
 
        //搜索 aba
        System.out.println(sb.indexOf("x"));
    }

}

 

回文串

import java.util.Scanner;

public class StringBufferDemo1 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        String str = sc.nextLine();
        StringBuffer sb = new StringBuffer(str);
        sb.reverse();
        if (str.equals(sb.toString())) {
            System.out.println("是一个回文字符串");
        } else {
            System.out.println("不是一个回文字符串");
        }
    }

}

 

Object

建议在类中重写这2个方法

    @Override
    public String toString() {
        return "name=" + name + "\tage=" + age + "\taddress=" + address;
    }
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Person) {
            Person other = (Person) obj;
            if (this.name.equals(other.getName()) && this.age == other.getAge()
                    && this.address.equals(other.getAddress())) {
                return true;

            } else {
                return false;
            }
        }
        return false;
    }    

 

posted @ 2017-03-13 19:14  摩西摩西点点  阅读(304)  评论(0编辑  收藏  举报