从 List<String> Java 解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12184616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:55:00  来源:igfitidea点击:

Parse from List<String> Java

javalistparsingarraylist

提问by Stephopolis

I am attempting to parse the value of the elements in a List declared as thus:

我试图解析 List 中元素的值,声明如下:

 List<String> uniqueList = new ArrayList<String>(dupMap.values());

The values are such as this:

这些值是这样的:

a:1-2
b:3-5

but I want one ArrayList with the first number (i.e. 1, 3) and another with the second (i.e. 2, 5). I have this worked out... Sorta:

但我想要一个带有第一个数字(即 1、3)的 ArrayList 和另一个带有第二个数字(即 2、5)的 ArrayList。我已经解决了... Sorta:

String delims= "\t"; String delim2= ":"; String delim3= "-";
String splits2[]; String splits3[]; String splits4[];
Map<String,String> dupMap = new TreeMap<String, String>();
List<String> uniqueList = new ArrayList<String>(dupMap.values());
ArrayList<String> parsed2 = new ArrayList<String>();
ArrayList<String> parsed3 = new ArrayList<String>();
ArrayList<String> parsed3two= new ArrayList<String>();
double uniques = uniqueList.size();
for(int a=0;a<uniques;a++){
    //this doesn't work like it would for an ArrayList
    splits2 = uniqueList.split(delim2) ;
    parsed2.add(splits2[1]);
    for(int q=0; q<splits2.length; q++){
        String change2 = splits2[q];
        if(change2.length()>2){
           splits3 = change2.split(delim3);
           parsed3.add(splits3[0]);
           String change3=splits3[q];
           if (change3.length()>2){
               splits4 = change3.split(delims);
               parsed3two.add(splits4[0]);
           }
        }
     }
  }

uniqueList.split does not work however and I don't know if there is a similar function for List. Is there any suggestions?

但是 uniqueList.split 不起作用,我不知道 List 是否有类似的功能。有什么建议吗?

回答by Roddy of the Frozen Peas

If you know that all of your data is in the form [something]:[num]-[num], you can use a regular expression like this:

如果你知道你的所有数据都在表单中[something]:[num]-[num],你可以使用这样的正则表达式:

Pattern p = Pattern.compile("^([^:]*):([^-]*)-([^-]*)$");

// I assume this holds all the values:
List<String> uniqueList = new ArrayList<String>(dupMap.values()); 

for (String src : uniqueList) {
    Matcher m = p.matcher(src); 
    if( m.find() && m.groupCount() >= 3) {
        String firstValue = m.group(1); // value to left of :
        String secondValue = m.group(2); // value between : and -
        String thirdValue = m.group(3); // value after -

        // assign to arraylists here
    }
}

I didn't actually put the code in to add to the specific ArrayLists because I couldn't quite tell from your code which ArrayListwas supposed to hold which value.

我实际上并没有将代码放入特定的ArrayLists 中,因为我无法从您的代码中完全分辨出哪个ArrayList应该包含哪个值。

Edit

编辑

Per Code-Guru's comment, an implementation using String.split()would go something like this:

根据 Code-Guru 的评论,使用的实现String.split()将是这样的:

String pattern = "[:\-]";

// I assume this holds all the values:
List<String> uniqueList = new ArrayList<String>(dupMap.values()); 

for (String src : uniqueList) {
    String[] parts = src.split(pattern);
    if (parts.length == 3) {
        String firstValue = parts[1]; // value to left of :
        String secondValue = parts[2]; // value between : and -
        String thirdValue = parts[3]; // value after -

        // assign to arraylists here
    }
}

Both approaches are pretty much the same in terms of efficiency.

这两种方法在效率方面几乎相同。

回答by Code-Apprentice

From what I understand of your question, I would proceed as follows:

根据我对你的问题的理解,我将继续如下:

for each String in uniqueList
    parse the string into a character and two integers (probably using a single call to [String.split()](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String, int))
    insert the first integer into an List
    insert the second integer into another List

This is in pseudocode. Translating into Java is left as an exercise to the reader.

这是伪代码。翻译成 Java 留给读者作为练习。