Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

java - Convert String into ArrayList<MyClass>

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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...