+ 2
Array replace
how to replace multiple array elements. I know how to remove an array element but the thing is if I remove one element the index will decrease but I want keep the index number.
5 Réponses
+ 16
An array is not mutable... How do you remove an element so that the indeces of the array are changed? This would mean you're changing the length of the array?
Maybe you mean an ArrayList? Then you could set the list element to null.
+ 3
Thanks Tashi N for your response. I mananged to use "Collection.addAll" method and its working perfectly.
+ 3
Assuming you were talking about an array:
Replace an element the same way you add one.
int[] a = new int[5];
a[0] = 4;
a[0] = 9; // replaced 4 with 9
For an arraylist, there's also a set(int, element) method.
int being the index of the element you want to replace, and the element being what you are replacing it with.
+ 2
Collections.addAll Method
0
Here is a code that replaces array element.
https://code.sololearn.com/cQ5prF6zVujo/?ref=app