java中传递数组中的代码出现意外输出

java中传递数组中的代码出现意外输出,java,arrays,parameter-passing,Java,Arrays,Parameter Passing,我在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);

我在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");
        }
    }
}
并删除了该行:

e = new Employee(); 
PrintOnScreen()

输出为:

名称:
年龄:21


以下代码中没有从阵列输出数据:

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");
    }
}

此外,以下代码没有意义:

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;
}

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

不确定,但我认为数组没有复制Java中带有“=”的内容。班级经营的变化

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

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

问题在于
sc.nextInt()
调用。这将从输入流中读取一个整数,但保留
回车
换行
符号。下一个
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
    }
    //..
}
publicstaticvoidmain(字符串[]args){
sc=新扫描仪(系统英寸);
打印(“输入数组的长度:\t”);
int n=sc.nextInt();

sc.nextLine();//对于(Employee e:this.emp)e=new Employee(),您想用
和
?请不要更改问题,以免使有效答案无效。使用更正的版本更改源代码是不正确的。是否也发布
员工
类的代码,以便我们可以查看getter/setter是否正确实现?我已经做了如您所说的更改,但o/p现在是:Name:Age:21这意味着Name是emptyWell,因为您没有向我们展示
Employee
的实现,我们不可能知道问题出在哪里。我最好的猜测是getter和setter根本不起作用。问题与数组复制无关。this.emp=emp;会复制内容吗?您听说过引用吗s
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");
    }
}
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
    }
    //..
}