+ 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.

6th Oct 2017, 3:50 PM
Sibusiso Mbambo
5 odpowiedzi
+ 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.
6th Oct 2017, 4:22 PM
Tashi N
Tashi N - avatar
+ 3
Thanks Tashi N for your response. I mananged to use "Collection.addAll" method and its working perfectly.
6th Oct 2017, 7:56 PM
Sibusiso Mbambo
+ 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.
6th Oct 2017, 8:20 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
Collections.addAll Method
6th Oct 2017, 7:57 PM
Sibusiso Mbambo
0
Here is a code that replaces array element. https://code.sololearn.com/cQ5prF6zVujo/?ref=app
6th Oct 2017, 9:23 PM
Sibusiso Mbambo