简体   繁体   中英

Converting string array to list: double brackets

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

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


    }
}

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]" !

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM