您的位置:首页 > Web前端

【JavaSE学习笔记】常用类介绍02_String,StringBuffer,Integer

2017-07-24 13:17 525 查看
常用类介绍02

A.String

1)上一章(常用类介绍01-C中已经介绍了String类大部分功能,本章介绍剩下小部分功能)

2)替换功能

String replace(char old, char new):将老的字符替换成一个新的字符

// 测试类
public class Demo01 {
public static void main(String[] args) {
String s = "helloworld";

String s2 = s.replace('h', 'H');
System.out.println(s2); // Helloworld
}
}


String replace(String old,String new):将老的字符串替换成新的字符串

// 测试类
public class Demo01 {
public static void main(String[] args) {
String s = "helloworld";

String s2 = s.replace("wor", "Wor");
System.out.println(s2); // helloWorld
}
}


String trim():去除字符串两端的空格

// 测试类
public class Demo01 {
public static void main(String[] args) {
String s = " hello world ";

String s2 = s.trim();
System.out.println(s2); // hello world
}
}


3)按字典顺序,比较两个字符串

int compareTo(String str):

// 测试类
public class Demo01 {
public static void main(String[] args) {
String s1 = "helloworld";
String s2 = "helloworld";
String s3 = "bcd";
System.out.println(s1.compareTo(s2)); // 0
System.out.println(s1.compareTo(s3)); // 6

System.out.println("-----------------");

String s4 = "hello";
String s5 = "hel";
System.out.println(s4.compareTo(s5)); // 2
}

//		 看compareTo()源码
//		  以s4,s5为例
public int compareTo(String anotherString) {
int len1 = value.length;	//this.value.length--->s4.toCharArray().length--->5
int len2 = anotherString.value.length;	//s5.toCharArray().length---->3
int lim = Math.min(len1, len2);		//int lim = Math.min(5,3) ;------>3
char v1[] = value;//s4字符串转换成字符数组:['h','e','l','l','0']
char v2[] = anotherString.value;//s5的字符数组:['h','e','l']

int k = 0;			//此处定义了一个临时变量
while (k < lim) {//k<3,0,1,2
char c1 = v1[k];	//['h','e','l']
char c2 = v2[k];	//['h','e','l']
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2;	//=====>char c1(s4) char c2(s5)=======>return s4.length-s5.length= 5-3 = 2
}

}
例如:

hello和hb:hb长度短,那长度都取2,判断:h一样,b和e不一样,就输出b和e的差

hello和he:he长度短,那长度都取2,判断:h一样,e一样,那就输出长度差

4)练习:

把arr[] = {1,2,3},按照[1,2,3]输出字符串类型

// 测试类
public class Demo01 {
public static void main(String[] args) {
// 先定义一个字符数组
int[] arr = { 1, 2, 3 };

String s = printArr(arr);
System.out.println(s);

}

// 方法输出
public static String printArr(int[] arr) {
// 定义一个空字符串
String s = "";
s += "[";
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
s += arr[i];
s += "]";
} else {
s += arr[i];
s += ", ";
}
}
return s;
}
}


B.StringBuffer

1)概述

线程安全的可变字符序列

线程安全---->同步---->效率低               比如:银行的网站、医院的网站

线程不安全---->不同步---->效率高        比如:论坛、博客、新闻

2)面试题

String和StringBuffer(线程安全),StringBulider(线程不安全)的区别?

共同点:都是字符串类型

不同点:

String:一旦被赋值,其值不能再改变,StringBuffer和StringBuilder是一个可变字符序列

StringBuffer:线程安全,同步,效率低,用在多线程中使用!

StringBulider:线程不安全,不同步,效率高;也是一个可变字符序列

一般用来替代StringBuffer,执行效率快!(单个线程中)

3)构造方法

public StringBuffer():构造一个不带字符串的字符缓冲区,初始容量为16个字符

// 测试类
public class Demo01 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
System.out.println(sb.length()); // 0
System.out.println(sb.capacity()); // 16 空的字符串缓冲区:初始默认容量为16个字符
}

}


public StringBuffer(int capacity):构造一个字符串缓冲区,指定容量

// 测试类
public class Demo01 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer(50);
System.out.println(sb.length()); // 0
System.out.println(sb.capacity()); // 50
}
}


public StringBuffer(String str):将str字符串创建到字符串缓冲区中:除适量16+str.length();

// 测试类
public class Demo01 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("hello");
System.out.println(sb.length()); // 5
System.out.println(sb.capacity()); // 21   16 + 5
}
}


注意:不能直接将一个字符串类型的值赋给StringBuffer

String s = "hello";

StringBuffer sb = s;   //错误的

StringBuffer sb = "hello";    //错误的

4)添加功能

public StringBuffer append(String str):表示在字符串缓冲中追加,返回字符串缓冲区本身

// 测试类
public class Demo01 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();

sb.append("hello");
sb.append(true);
sb.append('A');
sb.append(12.34F);
sb.append(100);
System.out.println(sb); // hellotrueA12.34100

// 链式编程
sb.append("hello").append(true).append('A').append(12.34F).append(100);

}
}


public StringBuffer insert(int offset, String str):在指定位置插入str字符串,返回字符串缓冲区本身

// 测试类
public class Demo01 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("helloworld");

sb.insert(5, " hi ");
System.out.println(sb);// hello hi world

}
}


5)删除功能

public StringBuffer deleteCharAt(int index):删除指定索引处的字符,返回StringBuffer

// 测试类
public class Demo01 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("helloworld");
StringBuffer sb1 = sb.deleteCharAt(5);// 删除w
System.out.println(sb1);// helloorld
}
}


public StringBuffer delete(int statr, int end):删除从指定位置开始,到指定位置结束,返回StringBuffer

// 测试类
public class Demo01 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("helloworld");
StringBuffer sb1 = sb.delete(0, 5);// 删除hello
System.out.println(sb1);// world
}
}


6)替换功能

public StringBuffer relpace(int start, int end, String str);

从指定位置开始到指定位置结束,替换的内容为一个新的字符串

// 测试类
public class Demo01 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append("hello");
sb.append("world");
sb.append("java");
System.out.println(sb);// helloworldjava
sb.replace(5, 10, " hi ");
System.out.println(sb);// hello hi java
}
}


7)反转功能

public StringBuffer reverse();

reverse():是StringBuffer特有功能,String没有

// 测试类
public class Demo01 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append(123456789);
System.out.println(sb);// 123456789
sb.reverse();
System.out.println(sb);// 987654321
}
}


8)截取功能

截取功能和前面几个功能的不同,现在返回的是新的字符串类型,而不是字符串缓冲区本身!

public String substring(int statr);

// 测试类
public class Demo01 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append(123456789);

String s = sb.substring(5);
System.out.println(s);// 6789
}
}


public String substring(int start, int end);

// 测试类
public class Demo01 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append(123456789);

String s = sb.substring(4, 8);
System.out.println(s);// 5678
}
}


9)面试题

StringBuffer和数组的区别

都是一个容器

数组:可以存储多个元素,这多个元素必须保证数据类型一致

StringBuffer:存储的始终都是一种数据类型,都是字符串

10)形式参数的问题

String类型作为形式参数和剧本数据类型是一样的

StringBuffer作为形式参数不一样

// 测试类
public class Demo01 {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";

change(s1, s2);
System.out.println(s1);// hello
System.out.println(s2);// world

StringBuffer sb1 = new StringBuffer("hello");
StringBuffer sb2 = new StringBuffer("world");
change(sb1, sb2);
System.out.println(sb1);// hello
System.out.println(sb2);// worldworld

}

public static void change(StringBuffer sb1, StringBuffer sb2) {
sb1 = sb2;
sb2.append(sb1); // 如果不传参,sb1 = world  s2 = worldworld
}

public static void change(String s1, String s2) {
s1 = s2;
s2 = s1 + s2; // 如果不试用传参,s1 = world s2 = worldworld
}
}
可以用反编译工具查看.class字节文件中的源码来分析

11)练习:判断一个字符串是否是一个对称字符串 aba 121

import java.util.Scanner;

// 测试类
public class Demo01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串:");
String s = sc.nextLine();

// 转换成字符缓存区,反转,再转换成String类型,最后再和s比较是否相等
boolean b = new StringBuffer(s).reverse().toString().equals(s);
System.out.println(b);

}
}


C.Integer

1)构造方法

public Integer(int value):构造一个新分配的Integer对象,它表示指定的 int 值

// 测试类
public class Demo01 {
public static void main(String[] args) {
int i = 100;

Integer i1 = new Integer(i);
System.out.println(i1); // 100
}
}


public Integer(String s):

throws NumberFormatException构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值

// 测试类
public class Demo01 {
public static void main(String[] args) {
String s = "100";

Integer i = new Integer(s);
System.out.println(i);// 100
}
}


2)进制转换功能和查询功能

// 测试类
public class Demo01 {
public static void main(String[] args) {
int i = 100;
// 转换二进制
System.out.println(Integer.toBinaryString(i));// 1100100
// 转换八进制//
System.out.println(Integer.toOctalString(i));// 144
// 转换16进制
System.out.println(Integer.toHexString(i));// 64

// int取值范围
System.out.println(Integer.MIN_VALUE);// -2146483648
System.out.println(Integer.MAX_VALUE);// 2147483647
}
}


3)扩展

为了让对基本数据类型进行更多的操作以及运算,所以java针对这种情况:就提供了基本数据类型包装类类型:

byte---------------------->Byte

short-------------------->Short

int------------------------>Integer

long--------------------->Long

float--------------------->Float

double------------------>Double

boolean---------------->Boolean

char--------------------->Character

4)int ----> String相互转换

// 测试类
public class Demo01 {
public static void main(String[] args) {
// int ----String
int i = 100;

//1)字符串拼接
String s1 = "" + i;
//2)int---Integet---String
Integer i2 = new Integer(i);
String s2 = i2.toString();
//3)public static String toString(int i)返回一个表示指定整数的 String 对象。
String s3 = Integer.toString(i);

//String ---- int
String ss = "100";

//1)String ---- Integer
int i1 = new Integer(ss).intValue();
//2)Integer类中的方法public static int parseInt(String s)
int i3 = Integer.parseInt(ss);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息