Accessing the Java Object Array Method

After developing in PHP for a long time, I decided to switch to Java. It’s convenient in OOP methodology and all that, I try to start from now on in java, but I get a hang when passing my arraylist object to a for statement, which will be printed using the methods of the Item class.

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 am I stuck inside HelloInventory.java

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

The IDE (Eclipse) gives me the error "Can only iterate over an array or java.lang.Iterable instance." Is there anything extra I need, or am I just going around this in Java? A proper example would be helpful.

Best, Levy

+4
source share
2 answers

You have a very strange architecture here, my friend. You should not use a generic Object everywhere, but actual types. First:

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

The List iteration now runs smoothly:

 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. The class name should point to a single entity, therefore, by convention, it is the only one.

Now don't do it wrong, but you may have gotten off the wrong foot with Java, so I highly recommend this reading: http://www.mindview.net/Books/TIJ/ This worked for me when I started with Java, maybe others can offer good sources.

+7
source

Ok, two things. Firstly, Tudor is absolutely right, it is better to use the classes that you expect directly, rather than objects, and stylistically his points are also accurate.

Two is that if you really need to use a list of objects, you will need to drop the object from any type that you expect to receive.

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

However, I would not recommend doing this, as it effectively accepts what should be a compile-time error, and makes it a RunTime error (if the class cannot be started).

+1
source

Source: https://habr.com/ru/post/1394815/


All Articles