Java 数组列表不兼容类型错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19483503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java arraylist incompatible types error
提问by Rainier
The answer to this will probably turn out to be obvious in retrospect but for now I find myself rather stuck on this. I'll give some blocks of code first and then present the problem.
回想起来,这个问题的答案可能会很明显,但现在我发现自己陷入了困境。我将首先给出一些代码块,然后提出问题。
This is part of my class Stockmanager, I have omitted some methods that have nothing to do with this problem.
这是我的 Stockmanager 类的一部分,我省略了一些与此问题无关的方法。
import java.util.ArrayList;
public class StockManager
{
private ArrayList stock;
public StockManager()
{
stock = new ArrayList();
}
public void addProduct(Product item)
{
stock.add(item);
}
public Product findProduct(int id)
{
int index = 0;
while (index < stock.size())
{
Product test = stock.get(index);
if (test.getID() == id)
{
return stock.get(index);
}
index++;
}
return null;
}
public void printProductDetails()
{
int index = 0;
while (index < stock.size())
{
System.out.println(stock.get(index).toString());
index++;
}
}
}
}
Here's my class Product, again with some methods omitted.
这是我的产品类,同样省略了一些方法。
public class Product
{
private int id;
private String name;
private int quantity;
public Product(int id, String name)
{
this.id = id;
this.name = name;
quantity = 0;
}
public int getID()
{
return id;
}
public String getName()
{
return name;
}
public int getQuantity()
{
return quantity;
}
public String toString()
{
return id + ": " +
name +
" voorraad: " + quantity;
}
}
}
My problem lies in the fact that I get a compile time error in the findProduct() method. To be more specific the line Product test = stock.get(index);
is indicated with a message incompatible types.
我的问题在于我在 findProduct() 方法中遇到编译时错误。更具体地说,该行Product test = stock.get(index);
用消息不兼容类型指示。
The constructor of StockManager creates a new ArrayList
with the name stock. As is evident from the method addProduct()
this ArrayList
contains items of the type Product
. The Product
class has a number of variables one of which is called id and is of type integer. That class also contains a method getID()
that returns an id.
StockManager 的构造函数创建了一个ArrayList
名为 stock的新对象。从方法中可以明显看出addProduct()
thisArrayList
包含类型的项目Product
。的Product
类有许多变量其中之一被称为ID和是整数类型。该类还包含一个getID()
返回id的方法。
As far as I know, the way of getting an item from an arraylist is the get()
method with the number between the () indicating the item's position. Seeing as my arraylist contains instances of Product
, I expect to get a Product
as result when I use the get()
method on the arraylist. So I don't understand why it doesn't work when I define a variable called test of the type Product and try to assign an item from the arraylist to it. I have as far as I know, successfully used this same technique in the method printProductDetails()
where I use the toString()
method from Product on the object from the arraylist.
据我所知,从arraylist中获取项的get()
方法是用()之间的数字表示项的位置的方法。由于我的 arraylist 包含 的实例Product
,Product
因此当我get()
在 arraylist 上使用该方法时,我希望得到一个结果。所以我不明白为什么当我定义一个 Product 类型的名为 test 的变量并尝试从 arraylist 将一个项目分配给它时它不起作用。据我所知,我在方法printProductDetails()
中成功地使用了相同的技术,在该方法中,我toString()
在 arraylist 中的对象上使用了Product 中的方法。
I hope someone will be able to clarify for me where I am at fault. If it makes any difference, I am doing this stuff in BlueJ which is probably not the best tool for it but it is the one I'm supposed to use for this school project.
我希望有人能够为我澄清我的错。如果它有什么不同,我正在 BlueJ 中做这些东西,它可能不是最好的工具,但它是我应该用于这个学校项目的工具。
采纳答案by Danstahr
Your stock
is defined as private ArrayList stock
, which means that stock.get()
returns an Object without any special type. You should either make Stock an ArrayList of Products
你stock
被定义为private ArrayList stock
,这意味着stock.get()
返回一个没有任何特殊类型的对象。您应该使 Stock 成为产品的 ArrayList
ArrayList<Product> stock;
or cast the result of the get
method manually
或get
手动转换方法的结果
Product test = (Product)stock.get(whatever);
回答by Alex
Product test = (Product) stock.get(index);
or if you make your list List<Product> stock
your line should work without changes.
或者,如果您列出清单,List<Product> stock
您的线路应该可以正常工作而无需更改。
回答by yamafontes
private ArrayList stock;
You should redeclare this with a bounded type like so:
您应该使用有界类型重新声明它,如下所示:
private List<Product> stock = new ArrayList<Product>();
If you don't, this line:
如果你不这样做,这一行:
Product test = stock.get(index);
won't work because you're trying to assign a raw Object
to a Product
.
将不起作用,因为您正试图将原始文件分配Object
给Product
.
Others have suggested casting the Object
to a Product
, but I wouldn't recommend this.
其他人建议将 the 转换Object
为 a Product
,但我不建议这样做。