gco*_*lby 2 java string stringbuilder object
我可能会走很长的路,希望我可以在没有代码的情况下解释这一点,因为代码到处都是.
我想要的是一个字符串
static String locationOneStr = new String ("res/.png");
static StringBuilder locationOneStrBuilder = new StringBuilder(locationOneStr);
Run Code Online (Sandbox Code Playgroud)
它包含我想稍后调用的URL的基本形式.
稍后出现的代码会找到位于0或'locationOne'的数字,并且交换机会选择要使用的代码:如果是0,则在.png之前插入0,如果它是1,则插入1等.
public static StringBuilder locationOneNumber(StringBuilder forCharConv, StringBuilder locationOneStrBuilder) {
char localChar = forCharConv.charAt(0);
switch (localChar) {
case '0':
BpmCalcFrame.locationOneStrBuilder.insert(3,"0");
System.out.println("Zero");
break;
/*
*
* Other Cases Omitted
*/
default:
System.out.println("There is no valid input!");
break;
}
return;
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是交换机必须返回一些东西.然而,我用作测试环境的jframe期望URL的字符串.
JLabel locationOne = new JLabel("Image 1");
locationOne.setBackground(Color.WHITE);
locationOne.setIcon(new ImageIcon(ArrayComparison.locationOneNumber(forCharConv, locationOneStrBuilder)));
locationOne.setBounds(172, 45, 36, 68);
contentPane.add(locationOne);
Run Code Online (Sandbox Code Playgroud)
我正在使用StringBuilder,因为Strings占用了内存,但是如果我必须将StringBuilder转换为String,那么它就无法使用StringBuilder
我是以错误的方式解决这个问题还是我真的必须转换回字符串?这意味着我将在整个程序的生命周期中平均每秒制作2个新对象.
有没有一种方法可以将StringBuilder的实例作为String而不创建新对象?
一秒钟担心两个新对象是过早优化.
您的JVM可能花费的时间少于十分之一毫秒.
编写首先清晰明显的代码,并在以后优化性能.
| 归档时间: |
|
| 查看次数: |
1145 次 |
| 最近记录: |