Convert String into ArrayList<MyClass>

耗尽温柔 提交于 2019-12-14 00:13:18

问题


I want to convert a String myString like that:

[ ["cd",5,6,7], ["rtt",55,33,12], ["by65",87,87,12] ]

into an ArrayList<CustomClass>

Where CustomClass have constructor :

public CustomClass (String name, int num1, int num2, int num3)

I tried first to create ArrayList of Strings :

List<String> List = new ArrayList<String>(Arrays.asList(myString.split("[")));

Didn't worked for me...

How can I get something like that:

List - {CustomClass,CustomClass,CustomClass,CustomClass}

first CustomClass = CustomClass.name="cd" , CustomClass.num1=5,CustomClass.num2=7...

second CustomClass = CustomClass.name="rtt",CustomClass.num1=55,CustomClass.num2=55...

and so on...


回答1:


You can do something like. If you cant guarantee the string formats then you may have to add additional checks for spliced array length and indexing.

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

class CustomClass {
    String name;
    int num1;
    int num2;
    int num3;

    public CustomClass(String name, int num1, int num2, int num3) {
        super();
        this.name = name;
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
    }
}

public class Sample {

    public static void main(String[] args) {
        String str = "[ [\"cd\",5,6,7], [\"rtt\",55,33,12], [\"by65\",87,87,12] ]";
        Pattern p = Pattern.compile("\\[(.*?)\\]");
        Matcher m = p.matcher(str.substring(1));
        List<CustomClass> customList = new ArrayList<CustomClass>();
        while (m.find()) {
            String[] arguments = m.group(1).split(",");
            customList.add(new CustomClass(arguments[0], 
                                            Integer.parseInt(arguments[1]), 
                                            Integer.parseInt(arguments[2]), 
                                            Integer.parseInt(arguments[3])));
        }
    }

}

Gson solution

public static void main(String[] args) {
    String json = "[ [\"cd\",5,6,7], [\"rtt\",55,33,12], [\"by65\",87,87,12] ]";
    List<CustomClass> customList = new ArrayList<CustomClass>();
    String[][] data = new Gson().fromJson(json, String[][].class);
    for (String[] strArray : data){
        customList.add(new CustomClass(strArray[0], 
                Integer.parseInt(strArray[1]), 
                Integer.parseInt(strArray[2]), 
                Integer.parseInt(strArray[3])));
    }
    System.out.println(customList);
}



回答2:


Here's a quick "by-hand" parser for you.

public static List<CustomClass> parseString(String str) {
    String [] elements = str.split("\\[");
    List <CustomClass> result = new ArrayList<CustomClass>();

    for (String elem : elements) {
        if (elem.contains("]")) {
            String seq = elem.substring(0, elem.indexOf(']'));
            String [] tokens = seq.split(",");
            result.add(new CustomClass(tokens[0].substring(1,tokens[0].length()-1),
                    Integer.parseInt(tokens[1]), 
                    Integer.parseInt(tokens[2]),
                    Integer.parseInt(tokens[3])));
        }
    }

    return result;
}

Beware that I relied on guaranteed correct input. You might want to tweak this one for what you need.



来源:https://stackoverflow.com/questions/24998055/convert-string-into-arraylistmyclass

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!