1

I have a situation where I want to convert a string into an ArrayList THAT CONTAINS INTEGERS AND OTHER ARRAYLISTS. In my string, I can have an arbitrary number of words which I want added into the ArrayList object:

String s = "[2,5,9,8,1,[5,7],9,8,[9,6,9,8],8,9]";

I did some work:

String testdata = "[25,645,[36,65],65]";
ArrayList<Integer> arrayInt = new ArrayList<>();
try (Scanner readFile = new Scanner(testdata)) {
    Pattern digitsPattern = Pattern.compile("(\\d+)");
    while (readFile.hasNextLine()) {
        Matcher m = digitsPattern.matcher(readFile.nextLine());
        while (m.find())
            arrayInt.add(Integer.valueOf(m.group(1)));
    }
}

The result is:

[25, 645, 36, 65, 65]

IWhat i want is this : [25,645,[36,65],65]

2 Answers 2

1

Remove [ and ], then use Scanner with , delimiter (or use [, ] and , as Scanner's delimiters - this regex should do the trick [\\[\\],]+).

Also to avoid Integer.parseInt use Scanner#nextInt which returns int.

EDIT:

If you are willing to accept list which will contain numbers as floating point types like doubles you could try JSON serialization/deserialization tools like gson.

String testdata = "[25, 645, [36, 65], 65]";
List fromJson = new Gson().fromJson(testdata, List.class);
System.out.println(fromJson);

Output: [25.0, 645.0, [36.0, 65.0], 65.0].

Sign up to request clarification or add additional context in comments.

7 Comments

think you for the answer , excuse me ibut what i want is to have an Arraylist THAT CONTAINS INTEGERS AND OTHER ARRAYLISTS
From your question: "result is: [25, 645, 36, 65, 65]", "Is there any way to do it in a simpler way..." suggests that result part from your question is correct and you are simply asking how to rewrite it using simpler code. What you said in comment above doesn't confirm your text from question. So please clarify what you are trying to do (what should be correct result since my approach would produce same as result from OP). BTW Lists are usually used to store single type of elements, If you want to have many levels of elements you should probably build some kind of three structure.
excuse me it's my fault that did not explain well , i did Edit my question what i want as result is this : [25,645,[36,65],65] think you verry much for your help
If you are willing to accept List which will contain numbers as floating points then you can use some JSON serialization tool. For instance with gson library code like new Gson().fromJson(testdata, List.class) would return ArrayList which will contain: [25.0, 645.0, [36.0, 65.0], 65.0].
think you for the idea of using json
|
1

An ArrayList<E> is typed, ie. you specify what single type <E> it can hold. For example, you have specified that arrayInt holds Integers, ie. E is Integer. This ArrayList cannot also then hold ArrayList<Integer>.

In order to meet your requirements you will need to define a type that can act as both a single Integer or a collection of Integers. This is usually implemented using the Composite Pattern.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.