Java中实现数组复制至空数组及循环检查marks数组的技术问题
Hey there! Let's tackle your Java array copying needs step by step. First, I notice your original code uses reference assignment (marks = Math;) which doesn't actually copy the array—it just makes marks point to the same array as Math. Let's fix that and implement proper array copying, then add the loop to check the marks array.
First, let's cover the common ways to copy an array to an empty target array (since initially marks is uninitialized, we need to create it with the right length first):
Using
System.arraycopy()(efficient, built-in method):// Example: Copy Math array to marks marks = new int[Math.length]; // Initialize marks with the same length as source array System.arraycopy(Math, 0, marks, 0, Math.length);Using
Arrays.copyOf()(cleaner, returns a new array):// Example: Copy Math array to marks marks = Arrays.copyOf(Math, Math.length);Note: Don't forget to import
java.util.Arrays;at the top of your file.Manual for-loop copy (great for custom logic):
marks = new int[Math.length]; for (int j = 0; j < Math.length; j++) { marks[j] = Math[j]; }
marks & Check with For Loop) Now let's update your code to use proper copying, then add the loop to check (e.g., print each element, or validate values):
import java.util.Arrays; // Needed if using Arrays.copyOf() public class MarkChecker { public static void main(String[] args) { int[] Math = {85, 65, 40, 20}; int[] English = {35, 55, 68, 75}; int[] ICT = {50, 35, 69, 95}; int i; int x = 1; // Change this to 2 or 3 to test other subjects int[] marks; // Properly copy the selected subject array to marks if (x == 1) { // Option 1: Using System.arraycopy() marks = new int[Math.length]; System.arraycopy(Math, 0, marks, 0, Math.length); // Option 2: Using Arrays.copyOf() (uncomment below, comment above) // marks = Arrays.copyOf(Math, Math.length); // Option 3: Manual loop (uncomment below, comment above) // marks = new int[Math.length]; // for (int j = 0; j < Math.length; j++) { // marks[j] = Math[j]; // } } else if (x == 2) { marks = Arrays.copyOf(English, English.length); // Using Arrays.copyOf for brevity } else if (x == 3) { marks = Arrays.copyOf(ICT, ICT.length); } else { // Handle invalid x value (default to empty array or throw error) marks = new int[0]; System.out.println("Invalid subject selection!"); } // For loop to check the marks array (e.g., print each element and pass/fail status) System.out.println("Checking marks array:"); for (i = 0; i < marks.length; i++) { // Example check: Print each mark and note if it's a pass (assuming 50 is passing) System.out.printf("Mark %d: %d - %s%n", (i+1), marks[i], marks[i] >= 50 ? "Pass" : "Fail"); } } }
Key Improvements:
- Real Array Copy: Instead of reference assignment, we create a new
marksarray and copy the elements from the subject array, so modifyingmarkswon't affect the original subject arrays. - Dynamic Length: We use
array.lengthinstead of hardcoding4, making the code flexible if you change the size of the subject arrays later. - Error Handling: Added an else clause to handle invalid
xvalues, preventing null pointer exceptions. - Meaningful Check: The loop now not only iterates through
marksbut also provides a useful check (pass/fail status) instead of an empty block.
内容的提问来源于stack exchange,提问作者Sachin Gimhan

