[英]How to retrieve information from an object I put into an arraylist
I created a class:我创建了一个 class:
public Inventario (int x, int y, int width, int height, BufferedImage sprite, String frutas) {}
This classe is to show an image (with width and height) at the x, y point on the screen.此类是在屏幕上的 x、y 点显示图像(具有宽度和高度)。 The sprite is the image itself and the String is my attempt of getting information by the name of the image saved.
精灵是图像本身,字符串是我尝试通过保存的图像名称获取信息。
So I add some objects to this class and put each object into an array list called "itensInventario":所以我向这个 class 添加了一些对象,并将每个 object 放入一个名为“itensInventario”的数组列表中:
Inventario ban = new Inventario(63, 27, 6, 6, sheet.getSprite(121, 171, 6, 6), "banana");
itensInventario.add(ban);
Inventario cen = new Inventario(10, 96, 6, 6, sheet.getSprite(121, 178, 6, 6), "cenoura");
itensInventario.add(cen);
The question is: how do I retrieve the String name of each object I put into the arraylist?问题是:如何检索我放入 arraylist 的每个 object 的字符串名称?
I tried to do something like this:我试图做这样的事情:
for(int i=0; i< Game.itensInventario.size(); i++) {
System.out.println("Show: " + Game.itensInventario.get(i));}
But I don't know how exactly get the String for each index "i".但我不知道如何准确获取每个索引“i”的字符串。
EDIT:编辑:
Inventário class (maybe it doesn't help. I tried to make a method GetFruta but in the end all objects would be with the same name in String so I gave up it but it still there): Inventário class (也许它没有帮助。我试图创建一个方法 GetFruta 但最后所有对象在 String 中都具有相同的名称,所以我放弃了它但它仍然存在):
public class Inventario公共 class 库存
{ {
public int dx;
public int dy;
public BufferedImage [] itens;
public static String fruta;
public Inventario (int x, int y, int width, int height, BufferedImage sprite, String frutas) {
itens = new BufferedImage [2];
itens[0] = Game.sheet.getSprite(121, 171, 6, 6); //Banana
itens[1] = Game.sheet.getSprite(121, 178, 6, 6); //Cenoura
fruta = frutas;
}
public void setFruta(String novaFruta) {this.fruta = novaFruta;}
public String getFruta() {return fruta;}
public void tick() {
for(int i=0; i< Game.itensInventario.size(); i++) {
System.out.println("esse: " + Game.itensInventario.get(i)+ " index: " +Game.itensInventario.get(i)+ "\n\n");}
}
public void render(Graphics g)
{
if(Game.itensInventario.size() >0) {
if( this==Game.itensInventario.get(0) ){
if(Game.itensInventario.get(0).getFruta() == "banana") { g.drawImage(itens[0], 63, Game.HEIGHT-27, null); }
else if(Game.itensInventario.get(0).getFruta() == "cenoura") { g.drawImage(itens[1], 63, Game.HEIGHT-27, null); }
}
if(Game.itensInventario.size() >1) {
if(this==Game.itensInventario.get(1) ){
if(Game.itensInventario.get(1).getFruta() == "banana") { g.drawImage(itens[0], 70, Game.HEIGHT-27, null);}
else if(Game.itensInventario.get(1).getFruta() == "cenoura") { g.drawImage(itens[1], 70, Game.HEIGHT-27, null); }
}
}
}
}
} }
EDIT 2编辑 2
I created this way the arraylist (List of objects from the class Inventario, ecah object has a String, right? Once I put this String in a new object how do I retrieve it?): I created this way the arraylist (List of objects from the class Inventario, ecah object has a String, right? Once I put this String in a new object how do I retrieve it?):
public static List<Inventario>itensInventario;
itensInventario = new ArrayList<Inventario>();
If you're trying to get your frutas
string, then it is as simple as如果你想得到你的
frutas
字符串,那么它就像
Game.itensInventario.get(i).getFruta();
However at the moment your fruta
field in the Inventario
class is static, which means it doesn't belong to that specific instance and is instead a shared variable amongst all instances.但是,目前您在
Inventario
class 中的fruta
字段是 static,这意味着它不属于该特定实例,而是所有实例之间的共享变量。 So removing the static modifier should solve your problem.因此删除 static 修饰符应该可以解决您的问题。
I would recommend taking a look at a Java tutorial to get a good grasp of some of the basics on static variables, like here: Understanding Class Members我建议看一下 Java 教程,以很好地掌握 static 变量的一些基础知识,例如: 了解 Class 成员
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.