I am splitting using
String[] arr = originalString.split(",");
Which is storing true, false, true and so on depending on what the user picked. How would I say just remove the false ones and leave the true values in the array?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Using your existing method, iterate through the returned array, set ‘false’ Strings to
null. This really doesn’t buy you much since you’re having to go through the array linearly and look at each element, which you could simply do when processing the responses. And since you can’t reduce the size of an array … you can’t really “remove” them in that sense. If you really need an array that contains only some of the responses, you’d need to copy the ones you want to a new array in this loop.Create a
Listfrom your array, remove items using its methods; the list will shrink and contain only what you want. Again … lots of linear traversing here, but you do end up with the result you’re looking for.Don’t use
String.split()in the first place. If you’re not interested in ‘false’ responses, use a regex via Java’sMatcherclass to extract only the ‘true’ respnonses from your original string.Change your approach. If these are user responses, don’t store them in a
String. Create aResponseobject that stores the user input as well as what they were responding to. Store these in twoLists, one for “true” responses, one for “false” responses.