更改java 1.4中的字符串列表代码



我需要在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);
            }
        }
        }

这样就有了java 1.4兼容格式的类。修改如下:

  • 没有泛型的ArrayList声明<>
  • 最后一个"for"循环使用Object变量而不是String变量,因为ArrayList返回一个Object引用,稍后可以将其转换为String。类代码如下: 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); } } }

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

相关内容

  • 没有找到相关文章

最新更新