将所有值分配给整个数组[重复]

时间:2021-11-20 13:09:12

This question already has an answer here:

这个问题在这里已有答案:

I am writing a Java program and I am using the following code for assigning some variables to an array:

我正在编写一个Java程序,我使用以下代码将一些变量分配给一个数组:

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?

谁知道如何在没有for循环的情况下将所有值分配给数组?

3 个解决方案

#1


5  

Use Arrays.fill(node.succList, value) javadoc. If you want to fill array with one value.

使用Arrays.fill(node.succList,value)javadoc。如果要使用一个值填充数组。

If you want to init array from the other one use

如果你想从另一个使用init数组

System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length) javadoc

System.arraycopy(Object src,int srcPos,Object dest,int destPos,int length)javadoc

#2


1  

Use System.arraycopy. Example as follows:

使用System.arraycopy。示例如下:

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

将输出:array2 = 0 10 20 30 40

#3


1  

As user khelwood pointed out, you can use System.arraycopy to copy elements of a specific range from one array to another.

用户khelwood指出,您可以使用System.arraycopy将特定范围的元素从一个数组复制到另一个数组。

The method signature is:

方法签名是:

public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
  • src is the source array which you want to copy
  • src是您要复制的源数组

  • srcPos is the starting position in the source array
  • srcPos是源数组中的起始位置

  • dest is the destination array you want to copy the data to
  • dest是要将数据复制到的目标数组

  • destPos is the starting position in the destination array
  • destPos是目标数组中的起始位置

  • length is the number of array elements copied
  • length是复制的数组元素的数量

For your case, it would be

对于你的情况,它会

System.arraycopy(node.succ.succList, 0, node.succList, 1, P.maxNetworkPow - 1);

Which means copy P.maxNetworkPow - 1 elements from node.succ.succList at starting index 0 to the array node.succList at starting index 1.

这意味着将P.maxNetworkPow - 从起始索引0处的node.succ.succList中的1个元素复制到起始索引1处的数组node.succList。

You have to be careful though, that P.maxNetworkPow is not bigger than the length of each array, or else you'll get an IndexOutOfBoundsException

你必须要小心,P.maxNetworkPow不大于每个数组的长度,否则你将获得IndexOutOfBoundsException

#1


5  

Use Arrays.fill(node.succList, value) javadoc. If you want to fill array with one value.

使用Arrays.fill(node.succList,value)javadoc。如果要使用一个值填充数组。

If you want to init array from the other one use

如果你想从另一个使用init数组

System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length) javadoc

System.arraycopy(Object src,int srcPos,Object dest,int destPos,int length)javadoc

#2


1  

Use System.arraycopy. Example as follows:

使用System.arraycopy。示例如下:

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

将输出:array2 = 0 10 20 30 40

#3


1  

As user khelwood pointed out, you can use System.arraycopy to copy elements of a specific range from one array to another.

用户khelwood指出,您可以使用System.arraycopy将特定范围的元素从一个数组复制到另一个数组。

The method signature is:

方法签名是:

public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
  • src is the source array which you want to copy
  • src是您要复制的源数组

  • srcPos is the starting position in the source array
  • srcPos是源数组中的起始位置

  • dest is the destination array you want to copy the data to
  • dest是要将数据复制到的目标数组

  • destPos is the starting position in the destination array
  • destPos是目标数组中的起始位置

  • length is the number of array elements copied
  • length是复制的数组元素的数量

For your case, it would be

对于你的情况,它会

System.arraycopy(node.succ.succList, 0, node.succList, 1, P.maxNetworkPow - 1);

Which means copy P.maxNetworkPow - 1 elements from node.succ.succList at starting index 0 to the array node.succList at starting index 1.

这意味着将P.maxNetworkPow - 从起始索引0处的node.succ.succList中的1个元素复制到起始索引1处的数组node.succList。

You have to be careful though, that P.maxNetworkPow is not bigger than the length of each array, or else you'll get an IndexOutOfBoundsException

你必须要小心,P.maxNetworkPow不大于每个数组的长度,否则你将获得IndexOutOfBoundsException