我需要在java 1.4中更改以下代码,任何人都可以帮我这样做。
我得到的主要问题是转换列表的字符串类型
line list splitStringList = new ArrayList()
和 行
for(String str:splitStringList)正在为java 1.4生成错误
public class Demo2ReferDemo1 {
public static void main (String [] args) {
String inputStr = "00400 - 00479,00100 - 0022200su,00100 - 00228,00100 - 00228,00400 - 00479,lab661,";
StringBuffer sb = new StringBuffer (inputStr);
List<String> splitStringList = new ArrayList<String> ();
boolean insideDoubleQuotes = false;
StringBuffer field = new StringBuffer ();
for (int i=0; i < sb.length(); i++) {
if (sb.charAt (i) == '"' && !insideDoubleQuotes) {
insideDoubleQuotes = true;
} else if (sb.charAt(i) == '"' && insideDoubleQuotes) {
insideDoubleQuotes = false;
splitStringList.add (field.toString().trim());
field.setLength(0);
} else if (sb.charAt(i) == ',' && !insideDoubleQuotes) {
// ignore the comma after double quotes.
if (field.length() > 0) {
splitStringList.add (field.toString().trim());
}
// clear the field for next word
field.setLength(0);
} else {
field.append (sb.charAt(i));
}
}
for (String str: splitStringList) {
System.out.println ("Split fields: "+str);
}
}
}
答案 0 :(得分:1)
JAVA 1.4不包含泛型,因此List<String> splitStringList = new ArrayList<String> ();
无效。
将其更改为List splitStringList = new ArrayList ();
你也会发现
for (String str: splitStringList) {
System.out.println ("Split fields: "+str);
}
会导致问题 - 这个语法也没有引入到Java 5,所以你必须回到其他一个循环(例如for
循环)并强制转换List成员到String
答案 1 :(得分:1)
这里有java 1.4兼容格式的类。变化是:
import java.util.ArrayList;
import java.util.List;
public class Demo2ReferDemo1 {
public static void main(String[] args) {
String inputStr = "00400 - 00479,00100 - 0022200su,00100 - 00228,00100 - 00228,00400 - 00479,lab661,";
StringBuffer sb = new StringBuffer(inputStr);
List splitStringList = new ArrayList();
boolean insideDoubleQuotes = false;
StringBuffer field = new StringBuffer();
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) == '"' && !insideDoubleQuotes) {
insideDoubleQuotes = true;
} else if (sb.charAt(i) == '"' && insideDoubleQuotes) {
insideDoubleQuotes = false;
splitStringList.add(field.toString().trim());
field.setLength(0);
} else if (sb.charAt(i) == ',' && !insideDoubleQuotes) {
// ignore the comma after double quotes.
if (field.length() > 0) {
splitStringList.add(field.toString().trim());
}
// clear the field for next word
field.setLength(0);
} else {
field.append(sb.charAt(i));
}
}
for (Object str : splitStringList) {
System.out.println("Split fields: " + str);
}
}
}