You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.

1. Proper Array Copying in Java

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];
    }
    
2.完善你的代码(Copy Subject Arrays to 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 marks array and copy the elements from the subject array, so modifying marks won't affect the original subject arrays.
  • Dynamic Length: We use array.length instead of hardcoding 4, making the code flexible if you change the size of the subject arrays later.
  • Error Handling: Added an else clause to handle invalid x values, preventing null pointer exceptions.
  • Meaningful Check: The loop now not only iterates through marks but also provides a useful check (pass/fail status) instead of an empty block.

内容的提问来源于stack exchange,提问作者Sachin Gimhan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:45:07