[英]remove all instances of item from the input array rather than just one
public static String[] remove(String[] symbols, String c)
{
for (int i = 0; i < symbols.length; i++)
{
if (symbols[i] == null ? c == null : symbols[i].equals(c))
{
String[] copy = new String[symbols.length-1];System.arraycopy(symbols, 0, copy, 0, i);
System.arraycopy(symbols, i+1, copy, i, symbols.length-i-1);
return copy;
}
}
return symbols;
}
當您從數組中刪除時,您不希望i
增加。 您也不想在循環之后返回任何內容。
更換線路
return copy;
和
symbols = copy;
i--;
該行return copy;
如果您想繼續刪除元素,則沒有任何意義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.