//知识点:java 中如何将 "字符串数组" 合并成 "一个字符串" 例如String [] ss = new String [n] 字符串数组中有n个
public static void main(String[] args) {
String[] str = new String[3];
str[0] = "first";
str[1] = "second";
str[2] = "third";
//方法1
String allStr1 = new String();
allStr1 = str[0].concat(str[1]).concat(str[2]);
//方法2
String allStr2 = new String();
allStr2 = str[0] + str[1] + str[2];
//方法3
StringBuffer sb = new StringBuffer();
String allStr3 = sb.append(str[0]).append(str[1]).append(str[2]).toString();
System.out.println(allStr1);
System.out.println(allStr2);
System.out.println(allStr3);
//方法4
StringBuffer sb1 = new StringBuffer();
String allStr4="";
for(int i=0;i<str.length;i++){
allStr4=sb1.append(str[i]).toString();
}
System.out.println(allStr4);
}
应用
/**
* 按消防栓主键查询出初始化数据,生成二维码
*/
@Override
public Integer insertQRCode(String hydrantId) {
//content= "http://www.zxhznkj.com/page/weizhi.html?fireplug=消防栓设备&number=消防栓编号&equipment=硬件01,触发器01;硬件02,触发器02;&hydrantId=1";
HydrantQRCodeEntity hydrantQRCodeEntity=hydrantQRCodeMapper.queryByIdHydrantQRCode(hydrantId);
String hydrantName=hydrantQRCodeEntity.getHydrantName();
String hydrantNumber=hydrantQRCodeEntity.getHydrantNumber();
List<HardwareQRCodeEntity> list=hardwareQRCodeMapper.queryByhydrantId(hydrantId);
String [] equipment=new String [list.size()];//创建字符数组,存放所有硬件设备
for(int i=0;i<list.size();i++){
HardwareQRCodeEntity hqrce=list.get(i);
String hardwareName=hqrce.getHardwareName();
String hardwareType=hqrce.getHardwareType();
String hardware=hardwareName+","+hardwareType+";";
equipment[i]=hardware; //取出的多个硬件存到数组中
}
//将字符串数组中的数据合并成一个字符串
String equipmentstr="";
StringBuffer sb = new StringBuffer();
for(int i=0;i<equipment.length;i++){
equipmentstr=sb.append(equipment[i]).toString();
}
String content="http://www.zxhznkj.com/page/weizhi.html?devicesName="+hydrantName+"&devicesNumber="+hydrantNumber+"&hydrantId="+hydrantId+"&equipment="+equipmentstr;
//调用生成二维码的接口,生成二维码
String imgPath="D:/upload/logo/logos.png"; //二维码logo
String destPath ="D:/upload/zxing/"; //生成二维码的地方
try {
QRCodeUtil.encode(content,imgPath,destPath, true);
} catch (Exception e) {
e.printStackTrace();
}
//修改二维码相关数据
hydrantQRCodeEntity.setQrCodeHost(content);
hydrantQRCodeEntity.setQrCodePicture(destPath);
hydrantQRCodeEntity.setQrCodeLogoimg(imgPath);
hydrantQRCodeEntity.setQrCodeStatus("未使用");
Integer iden=hydrantQRCodeMapper.updateHydrantQRCode(hydrantQRCodeEntity);
return iden;
}