Question #35485

Need to write a method, that takes an array of integers, an integer index, and an integer value. The method should set the array element with the specified index to the specified value.

My code: int setElement(int[]array) {
int key;
for(int i=0; i<array.length; i++) {
if(array[i] == key)
return i;
}
return key;
}
think it is not quite right...

Expert's answer

private void setElement(int[] array, int index, int value) {
    for(int i = 0; i < array.length; i++) {
        if(i == index)
            array[i] = value;
    }
}
LATEST TUTORIALS
APPROVED BY CLIENTS