1

I use this part of the code to get the select items from a jlist into a string list. I get the following result:

[[String1, String2, String3,...]]

How can I avoid the double []? Thanks

static List<String> strlist = new ArrayList<String>();

public class tog {

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

        final JList list = new JList(strlisttemp.toArray());
        list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        final ListSelectionListener listSelectionListener = new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {

                String lstr = list.getSelectedValuesList().toString();
            System.out.println(lstr);
                strlist = Arrays.asList(lstr.split("\\s*,\\s*"));

            }
        };

        list.addListSelectionListener(listSelectionListener);
        JOptionPane.showMessageDialog(null, list, "Select", JOptionPane.QUESTION_MESSAGE);

        System.out.println(strlist);

}

This is the part that has problem: When I print lstr it works properly [...]. When I use this:

strlist = Arrays.asList(lstr.split("\\s*,\\s*"));

Then System.out.println(strlist); prints double brackets

9
  • 6
    please format your code, it's almost unreadable Commented Oct 3, 2013 at 12:01
  • 2
    I think your missing the point list.getSelectedValuesList().toString(). You are not using the generic interface for JList and you are parsing the String when you can get a List<E> from getSelectedValuesList(). Commented Oct 3, 2013 at 12:05
  • Works fine otherwise: String str = "abc,def"; System.err.println(Arrays.asList(str.split("\\s*,\\s*"))); Commented Oct 3, 2013 at 12:24
  • What do you mean it works fine? Strlist shows the results like this [[aaa,bbb]]. I need [aaa,bbb]. @Optional Commented Oct 3, 2013 at 15:17
  • I believe the code that is vital in solving the problem is clearly readeable. @tom Commented Oct 3, 2013 at 15:19

3 Answers 3

2

You're unnecessarily converting the returned List to its raw String representation, splitting that String into a String[], then converting the String[] to List.

Instead, just work directly with the List which is returned.

package com.example.list;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class ListExample {
    static List<String> strlist = new ArrayList<String>();

    public static void main(String[] args) {
        List<String> strlisttemp = new ArrayList<String>();
        strlisttemp.add("a");
        strlisttemp.add("b");
        strlisttemp.add("c");

        final JList list = new JList(strlisttemp.toArray());
        list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        final ListSelectionListener listSelectionListener = new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {

                strlist = list.getSelectedValuesList(); // use this instead

//              String lstr = list.getSelectedValuesList().toString();
//              strlist = Arrays.asList(lstr.split("\\s*,\\s*"));

                System.out.println(strlist);
            }
        };

        list.addListSelectionListener(listSelectionListener);
        JOptionPane.showMessageDialog(null, list, "Select", JOptionPane.QUESTION_MESSAGE);


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

2 Comments

Yes but I need the info to end up a str array list. Thats why I use: strlist = Arrays.asList(lstr.split("\\s*,\\s*")); @rob
I'm not following. Do you need it in a String[] or a List<String>?
2

When you call List.toString it puts brackets around the result.

So you call list.getSelectedValuesList() and you are retrieving a List<String>. You call toString() on this which gives you "[a, b, c, d]". You then split this string on the comma-space sequence and get "[a", "b", "c", "d]". You put this into another list and call toString() on it and get another bracket around it.

It's double-bracketed because in this second list the first entry is "[a" and the last entry is "d]"!

Comments

1

This is to answer your question on why you're getting the double square brackets. For a better way to code this, see rob's answer.

String lstr = list.getSelectedValuesList().toString();

At this point, lstr == "[aaa,bbb,ccc]"

strlist = Arrays.asList(lstr.split("\\s*,\\s*"));

Now at this point, the first element of strlist will start with '[' because that is the first character of lstr, and the last element will end with ']' because that is the last character of lstr.

Now, when you go to print strlist

System.out.println(strlist);

Java will implicitly be calling list.toString(), which appends a set of square brackets when printing the list. Since the first element in your list starts with a square bracket, and the last element ends with one, what you end up with is a double bracket at the beginning and the end

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.