简体   繁体   中英

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;
}

When you remove from the array you do not want i to increase. You also do not want to return anything until after the loop.

Replace the line

return copy;

with

symbols = copy;
i--;

The line return copy; makes no sense if you want to continue to remove elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM