How to assign the same values to two arrays in Java?
In Java, you can use a loop to assign the same value to two arrays. Here is the specific implementation method:
int[] array1 = {1, 2, 3, 4, 5};
int[] array2 = new int[array1.length];
for (int i = 0; i < array1.length; i++) {
array2[i] = array1[i];
}
In the above code, an array array1 is first defined, and then an array array2 of the same length as array1 is created. Next, a for loop is used to iterate through each element in the array1 array and assign it to the corresponding position in array2.
After running the above code, array2 will have the same values as array1.