簡體   English   中英

從輸入數組中刪除 item 的所有實例,而不僅僅是一個

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM