简体   繁体   中英

Remove a duplicated array element but getting some error

When the code flow gets to the removeIf() logic the code breaks and it doesn't return anything as if it was a lang error. Please use the same code logic if possible.

public static SalmonSumario[] filterSummario(SalmonSumario[] salmon){
  Set<String> filter = new TreeSet<>();
  
  List<SalmonSumario> salmonSum = Arrays.asList(salmon);
  
  salmonSum.removeIf(array -> !filter.add(array.getSalmonNumber()));
  
  return salmonSum.toArray(new SalmonSumario[0]);
}

The list returned by Arrays.asList() has a fixed length so you can't add nor remove elements, Therefore, you will have to wrap it with new ArrayList<>(Arrays.asList(salmon))

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