Reputation: 39
public static void initialize(int A[], int initialValue) { ; }
this is the start of my code, asking how you can make it so that after running, every item in the array A becomes what I made initialValue. It's for a school assignment and the professor told us we can't use anything with an arrays function so no arrays.func
Thanks!
Upvotes: 2
Views: 169
Reputation: 841
This can be an another solution,
/*
* initialize a smaller piece of the array and use the System.arraycopy
* call to fill in the rest of the array in an expanding binary fashion
*/
public static void initialize(int A[], int initialValue) {
int len = A.length;
if (len > 0){
A[0] = initialValue;
}
for (int i = 1; i < len; i += i) {
System.arraycopy(A, 0, A, i, ((len - i) < i) ? (len - i) : i);
}
}
public static void main ( String [] args ) {
int A[] = new int[5];
initialize(A,1);
for (int i : A) {
System.out.println(A[i]);
}
}
OUTPUT: 1 1 1 1 1
Upvotes: -1
Reputation: 393856
You don't need that method.
Just call
Arrays.fill(A,initialValue);
BTW, your title has a mistake. Your array is a primitive array, so it won't contain references to initialValue
, it will contain that int
value multiple times.
Upvotes: 5