Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Object Array Foreach Method Access

After developing in PHP for a long time I have decided to step into Java. Comfortable in OOP methodology and all that, I'm trying to start off at that point within java, but I'm getting hung up on passing out my arraylist object into a for statement to be printed back out using the Item class methods.

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

}

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

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

}

Where I am stuck is within the HelloInventory.java

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

IDE (Eclipse) gives me the error "Can only iterate over an array or an instance of java.lang.Iterable". Is there something extra I need, or I'm I going around this totally the wrong way in Java? Correct example would be helpful.

Best, Levi

like image 443
LeviXC Avatar asked Feb 04 '12 23:02

LeviXC


2 Answers

You have a very strange architecture here my friend. You shouldn't be using generic Objects everywhere, but the actual types. First thing:

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

Why not just return the List itself?

public List<Item> getAllInventoryItems() {
     return this.InventoryItems;
}

Also change this:

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

and this:

public void setInventoryItems(Item inventoryItems) {
     this.InventoryItems.add(inventoryItems);
}

Now iterating the List is smooth sailing:

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

Btw, I changed Items to Item out of habit. A class name should indicate a single entity so by convention it's singular.

Now don't take this the wrong way, but you may have got off on the wrong foot with Java, so I highly recommend this reading: http://www.mindview.net/Books/TIJ/ This worked for me when I was starting with Java, maybe others can suggest some good sources as well.

like image 67
Tudor Avatar answered Oct 08 '22 00:10

Tudor


Ok, two things. One is that Tudor is absolutely right, it's best to use the classes you're expecting directly, not Objects, and stylistically his points are accurate too.

Two is that if you really have to use a list of object, you'll need to cast back from object to whatever type it is that you're expecting to receive.

List<Object> list = inv.getAllInventoryItems();
for (Object item : list){
    System.out.println((Items) item).getItemName();
}

However, I wouldn't recommend doing this as it effectively takes what should be a compile-time error and makes it a RunTime error (if the class cannot be cast).

like image 41
Alex Avatar answered Oct 08 '22 00:10

Alex