ok, so say i have an array of integers: int tX[h]; how would i dynamically add items to this array. like i press a button and it serches for the lowest array value (h) that equals 0 and puts a value into that slot.
-thanks ???
ok, so say i have an array of integers: int tX[h]; how would i dynamically add items to this array. like i press a button and it serches for the lowest array value (h) that equals 0 and puts a value into that slot.
-thanks ???
I really can’t make sense of what you’re trying to accomplish here lol.
I want to help, can you give more of a explanation? O_o
Really? This is hard to comprehend? He wants:
for(int i=0; i<arr.length; i++) {
if(arr[i]==0) {
arr[i] = value;
break;
}
}
When he says dynamically add items to the array, I thought he meant increasing the array size, in which case you could use an ArrayList. As far as the
[quote=“kingroka123,post:1,topic:42371”]
Riven’s solution is right.
(Wrapping it up into a function:)
public static void replace(int[] array, int replaceWith, int value) {
for (int i = 0; i < array.length; i++) {
if (array[i] == replaceWith) {
array[i] = value;
break; // stop searching
}
}
}
Call it like that:
int[] myarray = { 1, 2, 3, 4, 0, 6, 7, 99 };
replace(myarray, 0, 5);
// myarray now is { 1, 2, 3, 4, 5, 6, 7, 99 };