一、除了使用String类存储字符串之外,还可以使用StringBuffer类存储字符串。而且它是比String类更高效的存储字符串的一种引用数据类型。

优点:

  对字符串进行连接操作时,使用StringBuffer类可以大大提高程序的执行效率。

1、如何使用StringBuffer类

  StringBuffer类位于java.util包中,是String类的增强类。声明StringBuffer对象并初始化的方法如下:

    StringBuffer sb = new StringBuffer(“青春无悔”);

2、常用的StringBuffer类方法

  (1)、toString()方法

    String str = sb.toString();  //转换成String类

  (2)、append()方法

    字符串.append(参数);

  (3)、insert(位置,参数);

    insert();方法可以实现将参数插入到字符串的指定位置。

案例:编写一个方法,实现将一个数字字符串转换成逗号分隔的数字串。

 public class InsertDemo {

     public static void main(String[] args) {
String nums = "123418447.6717";
StringBuffer words = new StringBuffer(nums);
//向字符串插入逗号
int index = words.length();
if (words.lastIndexOf(".")>0) {
index = words.lastIndexOf(".");
}
for (int i = index-3; i >0; i=i-3) {
words.insert(i, ',');
}
System.out.println(words); } }

分析:利用StringBuffer类的length()方法获取字符串的长度,使用for循环从后往前每隔三位插入逗号。

二、String类、StringBuilder类及StringBuffer类对比

  1.String:字符串常量:不可变的对象,每次赋值等同于生成一个新的String对象。

  2.StringBuffer:字符串变量:可变的字符串。

  3.StringBuider:字符串变量:JDK1.5以后版本提供:单线程不同步。

三、StringBuffer类的应用:实现商品批发金额的显示

 import java.util.Scanner;

 /**
* 实现商品批发总金额的显示
* @author Administrator
*
*/
public class Goods {
//创建数组存放商品名及对应的价格
String[] goods = new String[]{"电风扇","洗衣机","电视机","冰箱","空调机"};
double[] price = new double[]{124.23,4500,8800.90,5000.88,4456,12000.46}; //登陆验证
private boolean login(String name, String pwd) {
boolean flag = false;
if (name.equals("TOM")&&pwd.equals("123")) {
System.out.println("登陆成功!");
flag = true;
}else{
System.out.println("用户名和密码不匹配!");
}
return flag;
}
//打印商品信息
private void show() {
//1.使用Map集合保存 //2.使用一般方法打印
System.out.println("*************欢迎进入商品批发城*************");
System.out.println("\t编号\t商品\t价格");
for (int i = 0; i < goods.length; i++) {
System.out.println("\t"+(i+1)+"\t"+goods[i]+"\t"+price[i]);
}
System.out.println("****************************************");
} //计算订单总价
private double calc(int goodId, int sum) {
//根据商品编号查找商品信息
int index = goodId-1;
double sumPrice = -1;
if (index>=0&&index < goods.length) {
sumPrice = price[index]*sum;
} else {
System.out.println("您选择的商品不存在!");
}
return sumPrice;
} //格式化输出金额
private boolean change(int goodId, int sum) {
//调用calc()方法获取商品金额
double d = this.calc(goodId, sum);
boolean flag = false;//商品不存在
if (d==-1) {
return flag;
}
StringBuffer price = new StringBuffer(String.valueOf(d));
for (int i = price.indexOf(".")-3; i > 0; i = i-3) {
price.insert(i, ',');
}
System.out.println("您需要付款:"+price);
return flag = true;
} //测试
public static void main(String[] args) {
Goods g = new Goods();
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
boolean flag = false;
do {
/*用户登录*/
System.out.print("请输入用户名:");
String name = input.next();
System.out.print("请输入密码:");
String pwd = input.next();
flag = g.login(name, pwd);
} while (!flag);
//打印批发商品信息
g.show();
flag = false;
do {
//接收用户批发操作
System.out.print("请输入您批发的商品编号:");
int goodId = input.nextInt();
System.out.print("请输入批发数量:");
int sum = input.nextInt();
//输出货款总额
flag = g.change(goodId,sum);
} while (!flag); } }

最新文章

  1. The Nine Indispensable Rules for HW/SW Debugging 软硬件调试之9条军规
  2. gulp问题
  3. H5、CSS3属性的支持性以及flex
  4. js 限制input输入字节长度
  5. NSQ:分布式消息队列学习记录
  6. [ZZ] D3D中的模板缓存(3)
  7. STM32F0xx_TIM基本延时配置详细过程
  8. 分享8款绚丽的HTML5/jQuery特效插件
  9. JavaScript引用类型(二)
  10. 循环中不要放入openSession()
  11. Hive 创建和生成Rcfile 和SequenceFile格式的表
  12. Csharp多态的实现概述
  13. java 学习心得
  14. Linux CentOS 7 防火墙/端口设置
  15. SpringBoot(四)之thymeleaf的使用
  16. input type date 解决移动端显示placeholder
  17. BZOJ3144[Hnoi2013]切糕——最小割
  18. tensorflow学习之(三)Varible 变量的使用
  19. 【BZOJ4036】[HAOI2015]按位或 FWT
  20. db2 批处理

热门文章

  1. loj #6235. 区间素数个数
  2. MyBatis与JDBC的对比
  3. 【python】10分钟教你用python一行代码搞点大新闻
  4. C++20草案中的宇宙飞船运算符(&lt;=&gt;,spaceship operator)
  5. jquery循环语句if-else if-else
  6. wpa_supplicant
  7. 用qt creator创建可继承ui类
  8. 【webservice】Two classes have the same XML type name(转)
  9. mac 添加环境变量
  10. 江西理工大学南昌校区排名赛 F: 单身狗的骑马游戏