I am writing a Java program and I am using the following code for assigning some variables to an array:
for(int j=1;j<P.maxNetworkPow;j++){
node.succList[j]=node.succ.succList[j-1];
}
Who knows how can I assign the all values to the array without for
loop?
Use Arrays.fill(node.succList, value)
javadoc.
If you want to fill array with one value.
If you want to init array from the other one use
System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
javadoc
Use System.arraycopy. Example as follows:
import java.lang.*;
public class SystemDemo {
public static void main(String[] args) {
int arr1[] = { 0, 1, 2, 3, 4, 5 };
int arr2[] = { 5, 10, 20, 30, 40, 50 };
// copies an array from the specified source array
System.arraycopy(arr1, 0, arr2, 0, 1);
System.out.print("array2 = ");
System.out.print(arr2[0] + " ");
System.out.print(arr2[1] + " ");
System.out.print(arr2[2] + " ");
System.out.print(arr2[3] + " ");
System.out.print(arr2[4] + " ");
}
}
will output: array2 = 0 10 20 30 40
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With