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

Java算法实现问题咨询:代码对比、输出异常及原理解析

Let's Fix Your Java Array Algorithm Issue

Hey there! Let's walk through your problem step by step—from why you're seeing that weird [I@3d4eac69 output, to clarifying your teacher's code, and comparing both approaches.

First: Why Are You Seeing [I@3d4eac69?

That string isn't an error—it's Java's default way of printing an array object. When you try to print an array directly (like System.out.println(ar);), Java outputs the array's hash code instead of its elements. To see the actual values, you need to either:

  • Loop through the array and print each element one by one
  • Use Arrays.toString(ar) (don't forget to import java.util.Arrays!)

Clarifying Your Teacher's Code

Your teacher's code uses a method called algo—let's break down the parts you're confused about:

1. public static int[] algo(int[] ar);

This is a method declaration (the method's "signature"). Let's unpack it:

  • public: The method can be called from anywhere in your code
  • static: You don't need to create an instance of the class to call this method (you can use it directly in main)
  • int[]: The method returns an integer array after processing
  • algo: The name of the method
  • int[] ar: The method takes an integer array as input (named ar)

In short: This method is designed to take an array, run your algorithm on it, and send back the modified array.

2. int []y=algo(x);

This line calls the algo method with your input array x, then stores the processed array that algo returns into a new variable y. The reason your teacher's code didn't show any output is simple—it was missing a print statement for y! Just add System.out.println(Arrays.toString(y)); after this line, and you'll see your expected result.

How the Algorithm Actually Runs

Let's map out the step-by-step execution of your logic to confirm the expected output:
Initial array: [12,8,23,6,5,17,20,9]

  • m=2, n=0: ar[2] = 12 + 8 = 20 → array becomes [12,8,20,6,5,17,20,9]
  • m=3, n=1: ar[3] = 8 + 20 = 28 → array becomes [12,8,20,28,5,17,20,9]
  • m=4, n=2: ar[4] = 20 + 28 = 48 → array becomes [12,8,20,28,48,17,20,9]
  • m=5, n=3: ar[5] = 28 + 48 = 76 → array becomes [12,8,20,28,48,76,20,9]
  • m=6, n=4: ar[6] = 48 + 76 = 124 → array becomes [12,8,20,28,48,76,124,9]
  • m=7, n=5: ar[7] = 76 + 124 = 200 → array becomes [12,8,20,28,48,76,124,200]
  • m=8, which is not less than the array length (8), so the loop stops. This matches your expected output perfectly.

Comparing Your Code vs. Your Teacher's Code

Your Original Code (Assumed Structure)

If your code was written directly in main without method encapsulation:

  • Pros: Super straightforward for beginners to follow—all logic is in one place, no extra layers.
  • Cons: Poor reusability—you can't easily run this algorithm on another array without copying the code. It also mixes business logic (the algorithm) with output logic, which makes the code messier as it grows.

Your Teacher's Code

  • Pros: Uses method encapsulation, which is a core Java best practice. The algorithm logic is isolated in algo, so you can call it as many times as you want with different arrays. It separates logic from output, making the code cleaner and easier to debug or extend.
  • Cons: The only issue was the missing print statement—this is just an oversight, not a flaw in the method design.

Correct Code Examples

Fixed Version of Your Code (Direct in Main)

import java.util.Arrays;

public class ArrayAlgorithm {
    public static void main(String[] args) {
        int[] ar = {12,8,23,6,5,17,20,9};
        int m = 2;
        int n = 0;
        
        while (m < ar.length) {
            ar[m] = ar[n] + ar[n+1];
            m++;
            n++;
        }
        
        // Print space-separated elements
        for (int num : ar) {
            System.out.print(num + " ");
        }
    }
}

Complete Version of Your Teacher's Code

import java.util.Arrays;

public class ArrayAlgorithm {
    public static void main(String[] args) {
        int[] x = {12,8,23,6,5,17,20,9};
        int[] y = algo(x); // Call the algo method
        // Print space-separated elements
        for (int num : y) {
            System.out.print(num + " ");
        }
    }
    
    // The algo method from your teacher
    public static int[] algo(int[] ar) {
        int m = 2;
        int n = 0;
        while (m < ar.length) {
            ar[m] = ar[n] + ar[n+1];
            m++;
            n++;
        }
        return ar; // Return the modified array
    }
}

Both of these will output your expected result: 12 8 20 28 48 76 124 200

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:18:17