2015-01-20 67 views
0

我想將所有輸入值傳遞給數組中的一個元素。我試圖玩數組,但不太明白他們如何處理多個輸入值。Java將多個輸入值存儲到單個數組元素

我已閱讀Oracle和這裏,但沒有真正脫穎而出,或他們求助於數組列表。

有沒有一種方法來封裝值,然後將它們存儲到數組?

任何幫助,非常感謝。

import java.util.Scanner; 

class Mobile 
{ 
private String name; 
private int number; 
private int quantity; 
private double cost; 

public Mobile(String name, int number, int quantity, double cost) 
{ 
    this.name = name; 
    this.number = number; 
    this.quantity = quantity; 
    this.cost = cost; 
} 
public String getName() 
{ 
    return name; 
} 
public void setName(String name) 
{ 
    this.name = name; 
} 
public int getNumber() 
{ 
    return number; 
} 
public void setNumber(int number) 
{ 
    this.number = number; 
} 
public int getQuantity() 
{ 
    return quantity; 
} 
public void setQuantity(int quantity) 
{ 
    this.quantity = quantity; 
} 
public double getCost() 
{ 
    return cost; 
} 
public void setCost(double cost) 
{ 
    this.cost = cost; 
} 
public double getValue() 
{ 
    return cost*quantity; 
} 
} 

public class MobilePhone 
{ 
public static void main (String args[]) 
{ 
    String itemName; 
    int itemNum; 
    int itemQuan; 
    double unitCost; 

    for(int i=0;i<5;i++) 
    { 
    Scanner input = new Scanner(System.in); 

    System.out.print("Enter Item Name: "); 
    itemName = input.nextLine(); 

    System.out.print("Enter Item Number: "); 
    itemNum = input.nextInt();   

    System.out.print("Enter Quantity of Item: "); 
    itemQuan = input.nextInt(); 

    System.out.print("Enter Price of Single Unit: "); 
    unitCost = input.nextDouble(); 

    Mobile m = new Mobile(itemName, itemNum, itemQuan, unitCost); 
    } 
} 
} 
+1

目前還不清楚你想做什麼 – Dici 2015-01-20 21:49:30

+0

我想將itemName,itemNum,itemQuan,unitCost的輸入值存儲到一個數組中。 – 2015-01-20 21:50:28

+1

那麼?是不是'移動[]'足夠呢? – Dici 2015-01-20 21:51:31

回答

3

我的理解是,你wantot把手機變成長度爲5,一個爲每個移動數組中輸入使。 所以你會聲明數組Mobile mobileValues[] = new Mobile[5];,並把每個值入陣,一旦你已經收集到的數據mobileValues[i] = new Mobile(itemName, itemNum, itemQuan, unitCost);

public static void main (String args[]) 
{ 
    String itemName; 
    int itemNum; 
    int itemQuan; 
    double unitCost; 
    Mobile mobileValues[] = new Mobile[5]; 

    for(int i=0;i<5;i++) 
    { 
     Scanner input = new Scanner(System.in); 
     System.out.print("Enter Item Name: "); 
     itemName = input.nextLine(); 
     System.out.print("Enter Item Number: "); 
     itemNum = input.nextInt();   
     System.out.print("Enter Quantity of Item: "); 
     itemQuan = input.nextInt(); 

     System.out.print("Enter Price of Single Unit: "); 
     unitCost = input.nextDouble(); 

     mobileValues[i] = new Mobile(itemName, itemNum, itemQuan, unitCost); 
    } 
} 
2

如果你有 Mobile m = new Mobile(itemName, itemNum, itemQuan, unitCost); 就是你,而不是想將它添加到您的數組或數組列表。

因爲你知道你會經歷值5次,你可以在main的頂部加上Mobile[] mobileArray = new Mobile[5];和其他的東西。

然後設置mobileArray[i] = new Mobile(...);

0

我不知道我理解的問題,我似乎沒有看到你的代碼中的任何數組或列表。 在任何情況下,您可能要做的一件事就是創建一個包含所有要放入數組元素的輸入的對象。然後將該對象放入數組元素中。 喜歡的東西:

class Token { 
    Input i1; 
    Input i2; 
    ... 
    public Token (Input a, Input b){ 
     i1=a; i2=b; 
    } 
    // Getters 
} 

然後創建令牌的數組如:

Token[] t = new Token[10] 

t[0]= new Token(a,b); 
1

因爲在你的代碼是從0-4做一個循環,你知道你有一個靜態數組中5個元素的大小。有了這樣的情況下,你可以使用聲明(靠近你的其他變量)爲對象的數組:

Mobile mobileValues[] = new Mobile[5]; 

然後,當你創建的,而不是將其分配給一個變量,新的移動物體,把它分配給如果你想保持你的對象聲明(這是沒有必要通過任何方式),對應於上最重要的循環中,您是(保留曲目由i在你的代碼。

mobileValues[i] = new Mobile(itemName, itemNum, itemQuan, unitCost); 

另外,指數,您可以執行以下操作:

Mobile m = new Mobile(itemName, itemNum, itemQuan, unitCost); 
mobileValues[i] = m; 

既然您知道可以添加的元素的數量,但這不是必要的,但是在更實際的現實世界中,您不會知道所需的元素數量,並且ArrayList會更適合這一點。ArrayList s時,可以定義(同樣,由您的其他變量)使用:

List<Mobile> mobileList = new ArrayList<Mobile>(); 

,然後在下面,當你創建對象,就可以直接使用它添加到list

mobileList.add(mobileValues[i] = new Mobile(itemName, itemNum, itemQuan, unitCost)); 

這很好地工作,因爲list s不要求您在創建它們時指定顯式大小,如array s。他們會根據您添加的元素數量來調整其大小。然後,您可以通過他們循環使用for循環類似於array S:

for(int i = 0; i < mobileList.size(); i++){ 
    // do stuff here 
} 

或使用迭代循環:

for(Mobile m : mobileList){ 
    // do stuff here 
} 

再次,這是沒有必要在您的解決方案使用list因爲你知道正確的尺寸,但它是更常見的需要,而不是一個array

相關問題