Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning all values to array on the whole [duplicate]

Tags:

java

arrays

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?

like image 457
sarah123 Avatar asked Aug 19 '16 08:08

sarah123


Video Answer


2 Answers

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

like image 111
Sergei Rybalkin Avatar answered Oct 13 '22 00:10

Sergei Rybalkin


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

like image 34
jarvo69 Avatar answered Oct 13 '22 00:10

jarvo69



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!