【问题标题】:unexpected output in code in passing array in javajava中传递数组时代码中的意外输出
【发布时间】:2014-10-30 07:35:11
【问题描述】:

我在 java 中传递数组时遇到了问题。代码是:

package input_output;

import static java.lang.System.out;
import java.util.Scanner;

public class InputOutput {
    private static Scanner sc;
    public static void main(String []args){
        sc = new Scanner(System.in);
        out.print("Enter the length of arrays   :\t");
        int n = sc.nextInt();
        Employee[] emp = new Employee[n];
        for(int i=0;i<n;i++){
            out.print("\nEnter name and age of " + (i+1) + " employee   :\t");
            emp[i] = new Employee();
            emp[i].setName(sc.nextLine());
            sc.nextLine();
            emp[i].setAge(sc.nextInt());
        }

        Operation operate = new Operation(emp,n);

        operate.printOnScreen();

    }
}

class Operation{ 
    Employee []emp;
    public Operation(Employee[] emp,int n){
        this.emp=emp;
        for(Employee e: this.emp)
          e = new Employee();
    }

    public void printOnScreen() { 
        for(Employee e : emp){
            e = new Employee();
            out.println("Name:\t" + e.getName() + "\t\tAge:\t" + e.getAge() + "\n");
        }
    }
}

Employee 类是一个简单的 bean,包含年龄和姓名的 getter 和 setter 方法。

输出来了:

名称:空
年龄:0

我的错误是什么?


现在我把构造函数改成了

public Operation(Employee[] emp, int n){
        this.emp=emp;
}

并删除该行:

e = new Employee(); 

来自PrintOnScreen()

输出是:

姓名:
年龄:21

【问题讨论】:

  • 你想用for(Employee e: this.emp) e = new Employee();达到什么目的?
  • 请不要更改问题,以免导致有效答案无效。用更正的版本更改源代码是不对的。
  • 是否也发布Employee 类的代码,以便我们查看getter/setter 是否正确实现?

标签: java arrays parameter-passing


【解决方案1】:

您没有在以下代码中从数组中输出数据:

public void printOnScreen() 
{ 
    for(Employee e : emp)
    {
        e = new Employee(); // <<< THIS IS WRONG, AS e IS ALREADY SET BY LOOP!!
        out.println("Name:\t" + e.getName() + "\t\tAge:\t" + e.getAge() + "\n");
    }
}

此代码在循环的每次迭代中创建一个新的e => 您不是从数组中输出条目,而是新创建的条目。代码应该是

public void printOnScreen() 
{ 
    for(Employee e : emp)
    {
        out.println("Name:\t" + e.getName() + "\t\tAge:\t" + e.getAge() + "\n");
    }
}

另外,下面的代码没有意义:

public Operation(Employee[] emp,int n){
    this.emp=emp;
    for(Employee e: this.emp)
      e = new Employee();
}

为什么要遍历 this.emp 中的所有员工以创建无处存储的 Employee 的新实例?此外,不使用n 参数。这段代码应该是:

public Operation(Employee[] emp)
{
    this.emp=emp;
}

【讨论】:

  • 我已经按照你所说的进行了更改,但是现在的 o/p 是:名称:年龄:21,这意味着名称为空
  • 好吧,由于您没有向我们展示Employee 的实现,我们无法判断问题出在哪里。我最好的猜测是 getter 和 setter 根本不起作用。
【解决方案2】:

在打印数据时,您正在打印新员工的数据,而不是您创建的早期员工对象。

【讨论】:

    【解决方案3】:

    不确定,但我认为 Array 不会在 Java 中复制带有“=”的内容。类操作的变化

    this.emp=emp;
    for(Employee e: this.emp)
        e = new Employee();
    

    for(int i=0; i<n; i++) {
        this.emp[i].setName(emp.getName());
        this.emp[i].setAge(emp.getAge());
    }
    

    然后改变

    operate.printOnScreen();
    

    operate.printOnScreen(emp);
    

    public void printOnScreen() { 
        for(Employee e : emp){
            e = new Employee();
            out.println("Name:\t" + e.getName() + "\t\tAge:\t" + e.getAge() + "\n");
        }
    }
    

    public void printOnScreen(Employee emp) { 
        for(Employee e : emp){
            out.println("Name:\t" + e.getName() + "\t\tAge:\t" + e.getAge() + "\n");
        }
    }
    

    【讨论】:

    • 问题与数组复制无关。
    • this.emp=emp;会复制内容吗?
    • 听说过参考资料吗?
    【解决方案4】:

    关于你的第二个问题:(空的name 字段)

    问题在于sc.nextInt() 调用。这会从输入流中读取一个整数,但会留下carriage returnline feed 符号。下一个sc.nextLine() 调用将读取这些符号,仅此而已。这样所有的名字都用\r\n填充。

    您可以通过以下方式解决此问题:

    public static void main(String []args){
        sc = new Scanner(System.in);
        out.print("Enter the length of arrays   :\t");
        int n = sc.nextInt();
        sc.nextLine(); // <-- new
        Employee[] emp = new Employee[n];
        for(int i=0;i<n;i++){
            out.print("\nEnter name and age of " + (i+1) + " employee   :\t");
            emp[i] = new Employee();
            emp[i].setName(sc.nextLine());
            //sc.nextLine(); // <-- removed 
            emp[i].setAge(sc.nextInt());
            sc.nextLine(); // <-- new
        }
        //..
    }
    

    或者

    public static void main(String []args){
        sc = new Scanner(System.in);
        out.print("Enter the length of arrays   :\t");
        int n = Integer.parseInt(sc.nextLine()); // <-- changed
        Employee[] emp = new Employee[n];
        for(int i=0;i<n;i++){
            out.print("\nEnter name and age of " + (i+1) + " employee   :\t");
            emp[i] = new Employee();
            emp[i].setName(sc.nextLine());
            //sc.nextLine(); // <-- removed 
            emp[i].setAge(Integer.parseInt(sc.nextLine())); // <-- changed
        }
        //..
    }
    

    如果这解决了您的第二个问题,请接受Thorsten Dittmars anwser 作为您的主要问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-10
      • 2016-11-14
      • 1970-01-01
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 2013-09-08
      • 1970-01-01
      相关资源
      最近更新 更多