我遇到了一些列表操作問題。我接受用戶的輸入並搜索它:如果我找到一個「=」符號,我假設它前面的字符串是一個變量的名稱,所以在該變量上面的那一行上,我想添加一個新的字符串用戶的輸入(在這種情況下,它被稱爲「tempVAR」,雖然並不重要)。我一直試圖用StringBuilder來做到這一點,但沒有任何成功,所以我目前正在嘗試使用ArrayLists來做到這一點,但我在向列表添加新元素時遇到困難。由於list.add(index,string)的工作方式,我所添加的元素右側的元素將始終爲其索引添加+1。有沒有辦法總是知道我正在尋找什麼索引,即使在添加了隨機數的字符串之後?這裏是我的代碼到目前爲止,如果你運行它,你會明白我的意思,而不是「tempVAR」或「tempVar1」被添加到變量的名稱上方,他們將被添加一個或錯誤的方式位置。添加到特定索引
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
public class ToTestStuff {
static List<String> referenceList = new ArrayList<String>();
public static final String SEMICOLUMN = ";";
public static final String BLANK = " ";
public static final String EMPTY = "";
public static final String LEFT_CURLY = "{";
public static final char CARRIAGE_RETURN = '\r';
public static final String CR_STRING = "CARRIAGE_RETURN_AND_NEW_LINE";
public static final char NEW_LINE = '\n';
public static void main(String[] args) {
List<String> test = new ArrayList<String>();
String x = "AGE_X";
String y = "AGE_Y";
String z = "AGE_YEARS";
String t = "P_PERIOD";
String w = "T_VALID";
referenceList.add(x);
referenceList.add(y);
referenceList.add(z);
referenceList.add(t);
referenceList.add(w);
String text2 = " if (AGE_YEARS > 35) {\r\n"
+ " varX = P_PERIOD ;\r\n"
+ " }\r\n"
+ " if (AGE_YEARS < 35) {\r\n"
+ " varY = T_VALID ;\r\n"
+ " varZ = AGE_Y ;\r\n"
+ " varA = AGE_X ;\r\n"
+ " }";
detectEquals(text2);
}
public static String detectEquals(String text) {
String a = null;
// text = text.trim();
// text = TestSplitting.addDelimiters(text);
String[] newString = text.split(" ");
List<String> test = Arrays.asList(newString);
StringBuilder strBuilder = new StringBuilder();
HashMap<String, List<Integer>> signs = new HashMap<String, List<Integer>>();
HashMap<String, List<Integer>> references = new HashMap<String, List<Integer>>();
HashMap<Integer, Integer> indexesOfStringAndList = new HashMap<Integer, Integer>();
List<String> testList = new ArrayList<String>();
List<Integer> lastList = new ArrayList<Integer>();
List<Integer> indexList = new ArrayList<Integer>();
List<Integer> refList = new ArrayList<Integer>();
List<String> keysList = new ArrayList<String>();
List<List> minList = new ArrayList<List>();
String previous = null;
int index = 0;
Object obj = new Object();
List<Integer> referenceValueList = new ArrayList<Integer>();
List<Integer> indexPosition = new ArrayList<Integer>();
String b = null;
int indexOfa = 0;
// System.out.println("a----> " + test);
List<String> anotherList = new ArrayList(test);
for (int i = 0; i < anotherList.size(); i++) {
a = anotherList.get(i).trim();
index = strBuilder.length();// - a.length();
// index = i;
strBuilder.append(a); // "=", 3 - if, 14 - while, 36 , "=", 15
testList.add(a);
if (a.equals("if") || a.equals("=")) {
lastList.add(i);
indexOfa = i;
indexesOfStringAndList.put(index, indexOfa);
refList.add(index);
indexPosition.add(index);
if (signs.containsKey(a)) {
signs.get(a).add(index);
} else {
signs.put(a, refList);
}
refList = new ArrayList<Integer>();
}
if (referenceList.contains(a)) {
indexList.add(index);
if (references.containsKey(a)) {
references.get(a).add(index);
} else {
references.put(a, indexList);
}
indexList = new ArrayList<Integer>();
}
}
for (String k : references.keySet()) {
keysList.add(k);
referenceValueList = references.get(k);
obj = Collections.min(referenceValueList);
int is = (Integer) obj;
ArrayList xx = new ArrayList();
xx.add(new Integer(is));
xx.add(k);
minList.add(xx);
}
for (List q : minList) {
Integer v = (Integer) q.get(0);
String ref = (String) q.get(1);
int x = closest(v, indexPosition);
int lSize = anotherList.size();
int sizeVar = lSize - test.size();
int indexOfPx = 0;
int px = 0;
if (x != 0) {
px = indexesOfStringAndList.get(x) - 1;
} else {
px = indexesOfStringAndList.get(x);
}
if (px == 0) {
System.out.println("previous when x=0 " +anotherList.get(px+sizeVar));
anotherList.add(px, "tempVar1=\r\n");
} else {
previous = anotherList.get(px + sizeVar);
System.out.println("previous is---> " + previous + " at position " + anotherList.indexOf(previous));
anotherList.add(anotherList.indexOf(previous) - 1, "\r\ntempVAR=");
}
}
strBuilder.setLength(0);
for (int j = 0; j < anotherList.size(); j++) {
b = anotherList.get(j);
strBuilder.append(b);
}
String stream = strBuilder.toString();
// stream = stream.replaceAll(CR_STRING, CARRIAGE_RETURN + EMPTY + NEW_LINE);
System.out.println("after ----> " + stream);
return stream;
}
public static int closest(int of, List<Integer> in) {
int min = Integer.MAX_VALUE;
int closest = of;
for (int v : in) {
final int diff = Math.abs(v - of);
if (diff < min) {
min = diff;
closest = v;
}
}
return closest;
}
}
我繪製了「=」和位置「如果」他們在StringBuilder的位置,但這些都是從當我試圖使用StringBuilder做我上面說的殘餘。
我一直在爲此奮鬥了幾天,仍然沒有設法做我需要的東西,我不知道我錯了哪裏。目前,我非常想讓這項工作(無論是列表還是字符串生成器)完成後,如果有更好的方法,我會研究並相應地進行調整。
addDelimiters()方法是我創建的一種方法,用於避免在「String text2」中看到字符串,但我爲此解決了這一問題,因爲它只會混亂我已經混亂的代碼,甚至更多:)不要認爲它與我爲什麼試圖做不起作用有任何關聯。
TLDR:在每個varX或varY或其他「var」前面的行我想能夠添加一個字符串到列表,但我認爲我的邏輯獲取變量名稱或添加到列表是錯的。
我很難搞清楚這個程序可能是什麼? – Jivings 2012-01-15 15:53:45
相信我我正試圖理解這個問題。我想我應該知道答案。就像許多其他人一樣。問題是你沒有把不必要的信息抽象出來,這讓我不僅相信我。如果你試圖簡化如下問題:我已經輸入了帶有字符串的列表,我想操縱它,在每個值Y之前在列表中添加值X(這是我理解的問題),有人可能會幫助你。 – 2012-01-15 16:03:48
謝謝你,我會努力將不必要的信息抽象出來。 – DVM 2012-01-15 16:12:53