私は長い道のりを歩んでいるかもしれませんが、コードがいたるところにあるので、コードなしでこれを説明できることを願っています.
私が欲しいのは文字列です
static String locationOneStr = new String ("res/.png");
static StringBuilder locationOneStrBuilder = new StringBuilder(locationOneStr);
これには、後で呼び出したい 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;
}
私が抱えている問題は、スイッチが何かを返さなければならないことです。ただし、テスト環境として使用している 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);
StringBuilder を使用しているのは Strings がメモリを消費するためですが、StringBuilder を String に変換する必要がある場合は、StringBuilder を使用するポイントが無効になります。
私はこれを間違った方法で行っていますか、それとも本当に文字列に戻す必要がありますか? これは、プログラムのライフサイクル全体で、1 秒あたり平均 2 つの新しいオブジェクトを作成することを意味します。
新しいオブジェクトを作成せずに StringBuilder のインスタンスを String として作成する方法はありますか?