Java对象阵列Foreach方法访问

Lev*_*iXC 4 java arrays arraylist object

在PHP开发很长一段时间后,我决定进入Java.在OOP方法和所有这些方面很舒服,我试图在java中开始这一点,但是我已经挂断了将我的arraylist对象传递给for语句,使用Item类方法打印出来.

HelloInvetory.java

package helloInventory;

import java.util.Arrays;

public class HelloInventory {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Object InvetoryItems;

        Inventory inv = new Inventory();
        inv.createItemObj(101, "camera", "Used camera that I bought off of a homeless guy.", 500);
        InvetoryItems = inv.getAllInventoryItems();

        for(Object item : InvetoryItems){
               System.out.println(item.getItemName());
        }

        System.out.println("Done");
    }

}
Run Code Online (Sandbox Code Playgroud)

Inventory.java

package helloInventory;

import java.util.*; 

/**
 * Tracks and maintains all items within the inventory
 * @author levi
 *
 */
public class Inventory {

    List<Object> InventoryItems = new ArrayList<Object>();

    /*
     * create object from Items class
     * and insert into Object[] array.
     */
    public void createItemObj(int sku, String name, String descriptor, float price) {
        Items item = new Items();
        item.setSku(sku);
        item.setItemName(name);
        item.setItemDescription(descriptor);
        item.setItemPrice(price);

        this.setInventoryItems(item);
    }
    public Object getAllInventoryItems() {
        //return InventoryItems;
         return this.InventoryItems.toArray();
    }

    public void setInventoryItems(Object inventoryItems) {
        //InventoryItems.add(inventoryItems);
         this.InventoryItems.add(inventoryItems);
    }
}
Run Code Online (Sandbox Code Playgroud)

Items.java

package helloInventory;
/**
 * Class object to hold each item details
 * @author levi
 *
 */
public class Items {

    int sku;
    String itemName;
    String itemDescription;
    float itemPrice;

    public int getSku() {
        return sku;
    }
    public void setSku(int sku) {
        this.sku = sku;
    }
    public String getItemName() {
        return itemName;
    }
    public void setItemName(String itemName) {
        this.itemName = itemName;
    }
    public String getItemDescription() {
        return itemDescription;
    }
    public void setItemDescription(String itemDescription) {
        this.itemDescription = itemDescription;
    }
    public float getItemPrice() {
        return itemPrice;
    }
    public void setItemPrice(float itemPrice) {
        this.itemPrice = itemPrice;
    }

}
Run Code Online (Sandbox Code Playgroud)

我陷入困境的地方是HelloInventory.java

for(Object item : InvetoryItems){
               System.out.println(item.getItemName());
        }
Run Code Online (Sandbox Code Playgroud)

IDE(Eclipse)给出了错误"只能迭代数组或java.lang.Iterable的实例".我需要一些额外的东西,或者我是否在Java中以错误的方式解决这个问题?正确的例子会有所帮助.

最好的,李维

Tud*_*dor 7

你的朋友在这里有一个非常奇怪的建筑.你不应该在Object任何地方使用泛型,而是实际的类型.第一件事:

public Object getAllInventoryItems() {
    //return InventoryItems;
     return this.InventoryItems.toArray();
}
Run Code Online (Sandbox Code Playgroud)

为什么不回归List自己呢?

public List<Item> getAllInventoryItems() {
     return this.InventoryItems;
}
Run Code Online (Sandbox Code Playgroud)

也改变这个:

List<Item> InventoryItems = new ArrayList<Item>();
Run Code Online (Sandbox Code Playgroud)

还有这个:

public void setInventoryItems(Item inventoryItems) {
     this.InventoryItems.add(inventoryItems);
}
Run Code Online (Sandbox Code Playgroud)

现在迭代List顺利航行:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    List<Item> InvetoryItems;

    Inventory inv = new Inventory();
    inv.createItemObj(101, "camera", "Used camera that I bought off of a homeless guy.", 500);
    InvetoryItems = inv.getAllInventoryItems();

    for(Item item : InvetoryItems){
           System.out.println(item.getItemName());
    }

    System.out.println("Done");
}
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我换Items到Item了习惯.类名应该表示单个实体,因此按惯例,它是单数.

现在不要采取这种错误的方式,但你可能已经用Java做错了,所以我强烈推荐这个阅读:http://www.mindview.net/Books/TIJ/这对我有用从Java开始,也许其他人也可以建议一些好的资源.