-1

这是我的字符串。[不是数组列表]

[1], [<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>], [22630]

如何将其转换为 Arraylist 如下

item 01 : [1]
item 02 : [<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>]
item 03 :  [22630]

已编辑

[1], [<2254... here also matched ", "
...0720,C,D>,<2254,890... here also matched ", "

所以分裂item 02不正确。

4

4 回答 4

3

这应该这样做

String s = "[1], [<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>], [22630]";

然后使用Arrays.asList()以及分隔符上的拆分", "

List<String> list = new ArrayList<String>(Arrays.asList(s.split(", ")));

对你的评论,你确定吗?

String itemTwo = "[<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>]";
System.out.println(itemTwo.equals(list.get(1)));

退货

true
于 2013-11-13T05:39:26.743 回答
2

这是使用正则表达式的更简单的解决方案。

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class TestRegularExpression2 {

    private static String input = "[1], [<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>], [22630]";

    public static void main(String[] args) {

        // Matches everything between the braces                
        Pattern pattern = Pattern.compile("\\[(.*?)\\]");
        Matcher matcher = pattern.matcher(input);   

        // Prints out each of the matching strings. 
        while (matcher.find()) {
            System.out.println("*" + matcher.group() + "*");
        }

    }

}

这个的输出是

*[1]*
*[<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>]*
*[22630]*

于 2013-11-13T09:25:45.043 回答
1
String str = "[1], [<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>], [22630]";

List<String> aList = new ArrayList<String>();

for (String s1 : str.split(", ")) {
    aList.add(s1);
}
于 2013-11-13T05:39:29.143 回答
0
import java.util.ArrayList;
import java.util.Arrays;


/**
 * @author JFVARUGH
 * here is my String. [NOT A Arraylist] [1],
 *  [1],
 *  [<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>], ...
 *
 *item 01 : [1]
  item 02 : [<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>]
  item 03 : [22630]
 */
public class StringProblel {

    @SuppressWarnings("null")
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int index =0, tempIndex=0,stringIndex=0;
        char[] keyWrds =  {'[',']',','};
        char[][] temp = new char[1000][1000];

        String sourceStr = "[1],[<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>,<2254,89014103211118510720,C,D>], [22630] ";

        while(index < sourceStr.length()){
            if( sourceStr.charAt(index) == keyWrds[0] ){
                temp[stringIndex][tempIndex] = sourceStr.charAt(index);
                while(sourceStr.charAt(index) != keyWrds[1] && index < sourceStr.length()){
                    index++;
                    temp[stringIndex][++tempIndex] = sourceStr.charAt(index);                   
                }

                stringIndex++;              
            }
            index++;
        }
        System.out.println(temp[1]);
    }

}


The time complexity will be O(n2). There is scope for improvements !

我已经遵守它正在工作。

于 2013-11-13T08:37:05.833 回答