[英]Returning StringBuilder as a String JAVA
I might be going the long way around and hopefully I can explain this without code as at the minute the code is all over the place. 我可能会走很长的路,希望我可以在没有代码的情况下解释这一点,因为代码到处都是。
What I want is a string 我想要的是一个字符串
static String locationOneStr = new String ("res/.png");
static StringBuilder locationOneStrBuilder = new StringBuilder(locationOneStr);
That contains a base form of the URL where I want to call to later. 它包含我想稍后调用的URL的基本形式。
The code that comes later finds the number residing at position 0 or 'locationOne' and a switch choses which code to utilize: if it is a 0 it inserts 0 before the .png if its a 1 it inserts a 1 etc. 稍后出现的代码会找到位于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;
}
The problem I have is that a switch has to return something. 我遇到的问题是开关必须返回一些东西。 However the jframe I am using as a test environment is expecting a String for the URL. 然而,我用作测试环境的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);
I am using StringBuilder as Strings eat up memory, but if I have to convert the StringBuilder to a String it defeats the point of using the StringBuilder 我正在使用StringBuilder,因为Strings占用了内存,但是如果我必须将StringBuilder转换为String,那么它就无法使用StringBuilder
Am I going about this the wrong way or do I really have to convert back to a String? 我是以错误的方式解决这个问题还是我真的必须转换回字符串? This would mean I will be making on average 2 new objects a second for the entire of the program's lifecycle. 这意味着我将在整个程序的生命周期中平均每秒制作2个新对象。
Is there a way of making an instance of the StringBuilder as a String without creating a new object? 有没有一种方法可以将StringBuilder的实例作为String而不创建新对象?
Worrying about two new objects a second is premature optimisation. 一秒钟担心两个新对象是过早优化。
Your JVM will likely spend less that a tenth of a millisecond doing that. 您的JVM可能花费的时间少于十分之一毫秒。
Write code that is clear and obvious first, and optimise for performance later. 编写首先清晰明显的代码,并在以后优化性能。
You're supposed to turn the StringBuilder
into a String
when you're done building the string. 在构建String
时,您应该将StringBuilder
转换为String
。 That's exactly how it's supposed to be used. 这正是应该如何使用的。
How about: 怎么样:
static String locationOneStrTemplate = "res{number}.png";
//...
public static StringBuilder locationOneNumber(StringBuilder forCharConv, String locationOneStrTemplate) {
//...
String actualURL = locationOneStrTemplate.replace("{number}", forCharConv);
//...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.