Remove a specific string from an array of string

前端 未结 5 1562
梦谈多话
梦谈多话 2020-12-03 13:45

I have an array like this:

String n[] = {\"google\",\"microsoft\",\"apple\"};

What I want to do is to remove \"apple\".

My problem

5条回答
  •  再見小時候
    2020-12-03 14:44

    Arrays in Java aren't dynamic, like collection classes. If you want a true collection that supports dynamic addition and deletion, use ArrayList<>. If you still want to live with vanilla arrays, find the index of string, construct a new array with size one less than the original, and use System.arraycopy() to copy the elements before and after. Or write a copy loop with skip by hand, on small arrays the difference will be negligible.

提交回复
热议问题