我有從文本文件中獲得的這一行和絃。例如,如何用不同的子字符串替換多個子字符串?
String chordLine = "C G Am C";
String transposedChordLine;
接下來,我需要用下面用兩個參數,轉置的String
弦和整數增量類移調chordLine
到一個新的transposedChordLine
。例如,transpose("C", 2)
將返回D
。
public class Transposer{
private int inc;
private static ArrayList<String> keysSharp;
private static ArrayList<String> keysFlat;
Transposer(){
keysSharp = new ArrayList<String>(Arrays.asList("C", "C#", "D", "D#","E", "F","F#", "G","G#", "A","A#", "B"));
keysFlat = new ArrayList<String>(Arrays.asList("C", "Db", "D", "Eb","E", "F","Gb", "G","Ab", "A","Bb", "B"));
}
public String transpose(String chord,int inc){
this.inc = inc;
String newChord;
if(chord.contains("/")){
String[] split = chord.split("/");
newChord = transposeKey(split[0]) + "/" + transposeKey(split[1]);
}else
newChord = transposeKey(chord);
return newChord;
}
private String transposeKey(String key){ // C#m/D# must pass C#m or D#
String nKey, tempKey;
if(key.length()>1){
nKey = key.substring(0, 2);
}
else{ nKey = key; }
int oldIndex, newIndex;
if(key.contains("b")){
oldIndex = (keysFlat.indexOf(nKey)>-1) ? keysFlat.indexOf(nKey) : keysFlat.indexOf(similarKey(nKey));
newIndex = (oldIndex + inc + keysFlat.size())%keysFlat.size();
tempKey = keysFlat.get(newIndex);
nKey = (key.length() < 3) ? tempKey : key.replace(nKey, tempKey);
//(nKey + key.substring(nKey.length(), key.length()));
}
else if(key.contains("#")){
oldIndex = (keysSharp.indexOf(nKey)>-1) ? keysSharp.indexOf(nKey) : keysSharp.indexOf(similarKey(nKey));
newIndex = (oldIndex + inc + keysSharp.size())%keysSharp.size();
tempKey = keysSharp.get(newIndex);
nKey = (key.length() < 3) ? tempKey : key.replace(nKey, tempKey);
}
else{
nKey = nKey.substring(0, 1);
oldIndex = (keysSharp.indexOf(nKey)>-1) ? keysSharp.indexOf(nKey) : keysSharp.indexOf(similarKey(nKey));
newIndex = (oldIndex + inc + keysSharp.size())%keysSharp.size();
tempKey = keysSharp.get(newIndex);
nKey = (key.length() < 2) ? tempKey : key.replace(nKey, tempKey);
}
return nKey;
}
private String similarKey(String nKey) {
String newKey;
switch(nKey){
case "Cb":
newKey = "B";
break;
case "Fb":
newKey = "E";
break;
case "E#":
newKey = "F";
break;
case "B#":
newKey = "c";
break;
default:
newKey = null;
}
return newKey;
}
}
如何在不損失空間的情況下更換chordLine
? 增量2應該有transposedChordLine="D A Bm D"
這裏是我當前的嘗試:
public static void main(String[] args) {
String chordLine = "C G Am C";
String transposedChordLine;
String normalize = chordLine.replaceAll("\\s+", " ");
String[] split = normalize.split(" ");
//System.out.println(normalize);
Transposer tran = new Transposer();
String[] temp = new String[split.length];
for(int i=0 ; i<split.length ; i++){
temp[i] = tran.transpose(split[i], 2);
//System.out.println(split[i]);
System.out.print(temp[i]);
}
transposedChordLine = chordLine.replaceAll([split], temp); //which is wrong
}
很多潛在的解決方案。例如:創建一個對象,即「ChordLine」,它將實例化單個和絃線。在這個對象的構造函數中,你可以顯式地跟蹤和絃'n_i'和'n_i + 1'之間的空白量。然後,在換位時,您可以簡單地替換輸出中的空白'ChordLine' – Kon
對不起,這不是關於你的問題,而是你的代碼非常糟糕。不要在構造函數中初始化靜態成員。將這兩個靜態變爲'List',並使用'Arrays.asList'直接初始化它們。另外,不要使用實例字段將參數傳遞給私有方法。如果'inc'在調用'transpose'時可能不同,則將其作爲參數傳遞給私有方法。如果'inc'始終是相同的,則將其傳遞給構造函數,而不是'transpose'方法。 –
Andreas
大約一年前我開始使用java。感謝您的評論。 – Tuss