Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (6.1k points)

I want to delete all the strings or objects equal to “a” in the array in the following code. How to do it?

foo[0] = "a"; 

foo[1] = "cc"; 

foo[2] = "a"; 

foo[3] = "dd";

1 Answer

0 votes
by (11.7k points)

You can try this Java code:

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

Collections.addAll(list, array);

list.removeAll(Arrays.asList("a"));

array = list.toArray(EMPTY_STRING_ARRAY);

If you want to become an expert in Java, check out this Java course from Intellipaat.

 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Oct 23, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer

Browse Categories

...