2014-09-05 39 views
2

我一直在java傳遞數組中遇到問題。代碼是:在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"); 
     } 
    } 
} 

僱員類是一個簡單的bean,包含年齡和名稱的getter和setter方法。

輸出來:

名稱:空
年齡:0

什麼是我的錯?


現在我已經改變了構造函數

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

和刪除行:

e = new Employee(); 

PrintOnScreen()

,輸出是:

名稱:
年齡:21

+0

你試圖用'for(Employee e:this.emp)e = new Employee();'來實現什麼? – Tom 2014-09-05 11:08:52

+0

請不要更改問題,以便使有效答案無效。使用更正後的版本更改源代碼是不正確的。 – 2014-09-05 11:17:57

+0

如何發佈'Employee'類的代碼,以便我們可以看到getter/setter是否被正確實現? – 2014-09-05 11:32:20

回答

4

你不是在下面的代碼從陣列輸出數據:

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

此外,下面的代碼沒有意義:在this.emp

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

爲什麼你遍歷所有的員工創建是Employee新實例存儲在哪裏?另外,不使用n參數。此代碼應閱讀:新員工的

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

我已經做了改變,因爲你說過,但現在o/p:名稱:年齡:21,這意味着名稱是空的 – sagar 2014-09-05 11:09:42

+0

好吧,因爲你沒有向我們展示'員工'的實施,這是不可能的我們要告訴問題在哪裏。我最好的猜測是getter和setter根本不起作用。 – 2014-09-05 11:15:15

1

雖然打印數據,要打印的數據,而不是您創建的早期員工對象。

0

不確定,但我認爲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"); 
    } 
} 
+0

該問題與陣列複製無關。 – 2014-09-05 11:15:57

+0

this.emp = emp;會複製內容? – 2014-09-05 11:27:10

+0

聽說過參考文獻嗎? – 2014-09-05 11:28:16

3

你的第二個問題:(空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 Dittmar小號anwser你的主要問題。