0

what would be the least verbose way to remove one item by value from char[] core java 8 > ?

char[] c = new char[] {'a', 'b', 'c'}; 

I want remove 'b' for egz.

domingo
  • 167
  • 8
  • 1
    Does this answer your question? [Removing an element from an Array (Java)](https://stackoverflow.com/questions/642897/removing-an-element-from-an-array-java) – Savior Apr 03 '20 at 20:15
  • 1
    Arrays have fixed size so please describe result you expect. Should it be new array with size reduced by amount of matching elements like `{'a', 'c'}`, or maybe something else like array which instead of matching elements have some special values like `#` for instance `{'a', '#', 'c'}`? – Pshemo Apr 03 '20 at 20:18
  • thanks for idea, that might work in some cases - and easy.. but yea i was more of how to remove an item in the way that array size is reduced. – domingo Apr 03 '20 at 20:57

1 Answers1

3

Not especially efficient, but certainly not verbose

char[] c = new char[] {'a', 'b', 'c'};
c = new String(c).replace("b","").toCharArray();
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • `getBytes()` -> `toCharArray()` –  Apr 03 '20 at 20:24
  • nice! just thinking about case when there are two 'b' and I want to remove only one of them.. :/ but probably that would be a different question. – domingo Apr 03 '20 at 20:31
  • @domingo you can use regular expressions for what you are suggesting – ControlAltDel Apr 03 '20 at 20:37
  • thanks, that would work. thought I'm more in array thinking as I want to manipulate array of chars, in TS or JS that seemed less verbose, easier to find index and remove by it, Also Java Arrays.streams is not there for char array but is there for other primitives.. anyway it is easy to write code to find index, but than remove by that index.. System.arraycopy maybe... Anyways there is an apache utils for a reason, xexe. – domingo Apr 03 '20 at 20:50