0

I am currently having a little problem removing a string from an array list. Code is as follows:

    String selectedCol = req.getParameter("col");
    if (selectedCol != null) {
        ArrayList < String > ar = new ArrayList < String > ();
        String removeColumn = layout.getColumn(selectedCol).getName();
        String layOut = "layout." + key + "." + layout.getName() + ".group";
        List < String > list = Arrays.asList(props.getProp(layOut, null).replace("[", "").replace("]", "").split(","));
        ar.addAll(list);
        ar.removeAll(Arrays.asList(removeColumn));
        props.putString(layOut, ar.toString().trim());
    }

so array "ar" populates the following:

[contact, created]

I thought it could be the list size which could be incorrect however it shows up with the correct size when I do ar.size()

The following variable:

removeColumn

is populated with "created".

I have tried the following:

ar.removeAll(Arrays.asList(removeColumn));

and

ar.remove(removeColumn);

which should result in:

[contact]

however using removeall or remove did not work. I am trying to achieve this without the need of looping through.

Help?

4
  • 6
    Can you please create an minimal reproducible example Commented May 10, 2017 at 12:25
  • well, I am confused what is happening what is expected? Commented May 10, 2017 at 12:26
  • 1
    does your array really contains the exact string you're triing to remove? If the case is different then this will not work. Commented May 10, 2017 at 12:29
  • @Nemisis I think you are right. I think the in the array is causing an issue hence why its not removing the string from the array. Commented May 10, 2017 at 12:30

2 Answers 2

4

You've generated the list from a string by splitting it on ",", but there are spaces between items in the string, so the two strings in your list are: "contact" and " created".

If you must parse a string in this way, change the split regex to ", ", or if you need it to be more tolerant to sloppy input, "\\s*,\\s*", allowing optional spaces before or after.

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

Comments

0

Your array elements may have some white spaces. Which is leading to mismatch. You can use “\s*,\s*” in split to get rid of those white spaces. Also use trim() to remove any leading or trailing spaces.

For Example:

    List < String > list = Arrays.asList(props.getProp(layOut, null).replace("[", "").replace("]", "").trim().split("\\s*,\\s*"));

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.