1、StringBuffer 类
1.1、定义
package stringdemo;
public class StringBuffer01 {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("hello");
System.out.println(stringBuffer);
}
}
1.2、String VS StringBuffer
1.3、String 和 StringBuffer 相互转换
package stringdemo;
public class StringAndStringBuffer {
public static void main(String[] args) {
String str = "hello tom";
StringBuffer stringBuffer = new StringBuffer(str);
StringBuffer stringBuffer1 = new StringBuffer();
stringBuffer1.append(str);
StringBuffer stringBuffer3 = new StringBuffer("你好世界");
String s = stringBuffer3.toString();
String s1 = new String(stringBuffer3);
}
}
1.4、StringBuffer 类常见方法
package stringdemo;
public class StringBufferMethod {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("hello");
stringBuffer.append(',');
stringBuffer.append("java");
stringBuffer.append('、').append("python").append('、').append("php");
System.out.println(stringBuffer);
stringBuffer.delete(11, 14);
System.out.println(stringBuffer);
stringBuffer.replace(11, 14, "go");
System.out.println(stringBuffer);
System.out.println(stringBuffer.indexOf("c#"));
System.out.println(stringBuffer.indexOf("java"));
stringBuffer.insert(11, "c#、");
System.out.println(stringBuffer);
int length = stringBuffer.length();
System.out.println("length=" + length);
}
}
1.5、StringBuffer 类小练习
package stringdemo;
public class StringBufferExercise01 {
public static void main(String[] args) {
String str = null;
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(str);
System.out.println(stringBuffer.length());
System.out.println(stringBuffer);
StringBuffer stringBuffer1 = new StringBuffer(str);
System.out.println(stringBuffer1);
}
}
package stringdemo;
public class StringBufferExercise02 {
public static void main(String[] args) {
String price = "8123564.59";
StringBuffer stringBuffer = new StringBuffer(price);
for (int i = stringBuffer.lastIndexOf(".") - 3; i > 0; i -= 3) {
stringBuffer = stringBuffer.insert(i, ",");
}
System.out.println(stringBuffer);
}
}
2、StringBuilder 类
2.1、定义
package stringdemo;
public class StringBuilder01 {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder();
}
}
2.2、StringBuilder 常用方法
2.3、String、StringBuffer 和 StringBuilder 的比较
package stringdemo;
public class StringVsStringBufferVsStringBuilder {
public static void main(String[] args) {
long startTime = 0L;
long endTime = 0L;
StringBuffer buffer = new StringBuffer("");
startTime = System.currentTimeMillis();
for (int i = 0; i < 80000; i++) {
buffer.append(String.valueOf(i));
}
endTime = System.currentTimeMillis();
System.out.println("StringBuffer 的执行时间:" + (endTime - startTime));
StringBuilder builder = new StringBuilder("");
startTime = System.currentTimeMillis();
for (int i = 0; i < 80000; i++) {
builder.append(String.valueOf(i));
}
endTime = System.currentTimeMillis();
System.out.println("StringBuilder 的执行时间:" + (endTime - startTime));
String text = "";
startTime = System.currentTimeMillis();
for (int i = 0; i < 80000; i++) {
text = text + i;
}
endTime = System.currentTimeMillis();
System.out.println("String 的执行时间:" + (endTime - startTime));
}
}
2.4、String、StringBuffer 和 StringBuilder 的选择
3、Math 类
3.1、定义
Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数
3.2、方法一览(均为静态方法)
3.3、Math 类常见方法
package stringdemo;
public class MathMethod {
public static void main(String[] args) {
int abs = Math.abs(-9);
System.out.println(abs);
double pow = Math.pow(2, 4);
System.out.println(pow);
double ceil = Math.ceil(3.9);
System.out.println(ceil);
double floor = Math.floor(4.001);
System.out.println(floor);
long round = Math.round(5.51);
System.out.println(round);
double sqrt = Math.sqrt(9.0);
System.out.println(sqrt);
for (int i = 0; i < 100; i++) {
System.out.println((int) (2 + Math.random() * (7 - 2 + 1)));
}
int min = Math.min(1, 9);
int max = Math.max(45, 90);
System.out.println("min=" + min);
System.out.println("max=" + max);
}
}
|