在Java中,声明数组后赋值有多种方法,以下是几种常见的方式:
直接赋值
int[] arr = {1, 2, 3, 4, 5};
逐个元素赋值
int[] arr = new int;
arr = 1;
arr = 2;
arr = 3;
arr = 4;
arr = 5;
使用循环赋值
int[] arr = new int;
for (int i = 0; i < arr.length; i++) {
arr[i] = i + 1;
}
使用`System.arraycopy()`方法
int[] source = {1, 2, 3, 4, 5};
int[] target = new int;
System.arraycopy(source, 0, target, 0, source.length);
使用`Arrays.asList()`方法
import java.util.Arrays;
Integer[] numbers = Arrays.asList(1, 2, 3, 4, 5).toArray(new Integer);