简体   繁体   中英

adding a class object to a array

i am writing aa program that has two classes. the first class has an array which will be able to add the other class into its array with a length of 10. however i get the error saying "Cannot find Symbol". so my question is how do i add a seperate class to the array of an another class?

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Arrays;

public class HomeInventoryManager

private int[] listOfInventoryItems;
/**
 * Initialise the home inventory manager.
 */

public HomeInventoryManager()
{
    listOfInventoryItems = new int[10];
    InventoryItem = 0;  **i get the error here saying it cannot find the symbol variable InventoryItem** 
}

public void addInventoryItem()
{
    listOfInventoryItems[InventoryItem] = anInt;
    inventoryItem++;
}

UML图

That's obvious. You haven't declared what is inventoryItem . It whould be declared at the top of the class similar to the declaration of listofInventoryItems

public class HomeInventoryManager{

    private int[] listOfInventoryItems;
    private int inventoryItem;
    /**
     * Initialise the home inventory manager.
     */
    public HomeInventoryManager()
    {
        listOfInventoryItems = new int[10];
        inventoryItem = 0;
    }
    //your code here
}

Also, in the other 2 methods, you have 2 spellings for inventoryItem. Correct it also.

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Arrays;

public class HomeInventoryManager{


private int[] listOfInventoryItems;
private int InventoryItem;

//constructor
public HomeInventoryManager()
{
    listOfInventoryItems = new int[10];
    InventoryItem = 0;
}

public void addInventoryItem()
{
    listOfInventoryItems[0] = anInt; 
    inventoryItem++;
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM