[英]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". 第一个类具有一个数组,该数组将能够将另一个类添加到其长度为10的数组中。但是,我收到错误消息,提示“找不到符号”。 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++;
}
That's obvious. 很明显 You haven't declared what is inventoryItem . 您尚未声明什么是stockingItem 。 It whould be declared at the top of the class similar to the declaration of listofInventoryItems 它应该在类的顶部进行声明, 类似于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. 另外,在其他2种方法中,stockingItem具有2个拼写。 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++;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.