Java新手求助:如何重复向一维及二维数组添加相同元素
Hey there! As a fellow Java learner, I get how tricky array operations can be when you're just starting out. Let's walk through exactly how to repeat elements (for 1D arrays) or rows (for 2D arrays) N times like you described.
The goal here is to create a new array that's N times the length of the original, with the original element sequence repeated N times. The most efficient way to do this uses System.arraycopy() (a native method that's faster than manual loop copying), but I'll break it down step by step:
public class Repeat1DArrayExample { public static void main(String[] args) { int[] original = {1, 2, 3}; int repeatTimes = 2; // Change this to any positive integer int[] repeatedArray = repeat1D(original, repeatTimes); // Print the result to verify for (int num : repeatedArray) { System.out.print(num + " "); } // Output: 1 2 3 1 2 3 } private static int[] repeat1D(int[] original, int times) { // Handle edge cases: empty original array or invalid repeat count if (original == null || times <= 0) { return new int[0]; } int originalLength = original.length; int[] result = new int[originalLength * times]; // Loop N times, copying the original array into the result each time for (int i = 0; i < times; i++) { // Copy from original array (start at index 0) to result array // Starting position in result: i * originalLength System.arraycopy(original, 0, result, i * originalLength, originalLength); } return result; } }
Quick Explanation:
- We first check for edge cases to avoid null pointers or invalid array lengths.
- We create a new array with length equal to
original length * N. - Using
System.arraycopy(), we copy the entire original array into the result array N times, shifting the starting position each time to avoid overwriting previous copies.
For 2D arrays, we want to repeat the entire set of rows N times. The logic is similar to the 1D case, but we're copying entire rows instead of individual elements:
public class Repeat2DArrayExample { public static void main(String[] args) { int[][] original = {{1,2,3}, {4,5,6}, {7,8,9}}; int repeatTimes = 2; int[][] repeatedArray = repeat2D(original, repeatTimes); // Print the result for (int[] row : repeatedArray) { for (int num : row) { System.out.print(num + " "); } System.out.println(); } /* Output: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 */ } private static int[][] repeat2D(int[][] original, int times) { if (original == null || times <= 0) { return new int[0][]; } int originalRowCount = original.length; int[][] result = new int[originalRowCount * times][]; // Copy the original rows into the result array N times for (int i = 0; i < times; i++) { System.arraycopy(original, 0, result, i * originalRowCount, originalRowCount); } return result; } }
Quick Explanation:
- Again, we handle edge cases first.
- The new 2D array has
original row count * Nrows. System.arraycopy()works here too—it copies the row references from the original array to the result array. Since we're dealing withintarrays (primitive types), we don't need to worry about deep copying here (if you were working with object arrays, you'd need to copy each row's elements individually to avoid shared references).
Bonus: Manual Loop Alternative (If You Want to See the Low-Level Logic)
If you prefer to write the copy logic manually (without System.arraycopy()), here's how you'd do the 1D version:
private static int[] repeat1DManual(int[] original, int times) { if (original == null || times <= 0) { return new int[0]; } int originalLength = original.length; int[] result = new int[originalLength * times]; for (int i = 0; i < times; i++) { for (int j = 0; j < originalLength; j++) { result[i * originalLength + j] = original[j]; } } return result; }
This does the same thing, but System.arraycopy() is more efficient for large arrays.
内容的提问来源于stack exchange,提问作者FlyingBurger

