英文:
Can't access object properties inside Arraylist inside another Arraylist, Java
问题
这是创建对象的类:
class cards_type{
String color;
String number;
public cards_type(final String input_color, final String input_number) {
color = input_color;
number = input_number;
}
}
这是ArrayList:
ArrayList<ArrayList<cards_type>> players_cards = new ArrayList<ArrayList<cards_type>>();
players_cards.add(new ArrayList<cards_type>());
当我使用以下代码添加值并打印时,它返回对象:
System.out.println(players_cards.get(0).get(0));
输出:cards_type@ea4a92b
但如果我尝试访问属性,就会出现错误,就好像它不是一个对象:
System.out.println(players_cards.get(0).get(0).color);
输出:Exception in thread "main" java.lang.Error: Unresolved compilation problem:
color cannot be resolved or is not a field
感谢您的帮助。
英文:
This is the class that creates the objects:
class cards_type{
String color;
String number;
public cards_type(final String input_color, final String input_number) {
color = input_color;
number = input_number;
}
}
And this are the arraylist:
ArrayList<ArrayList>players_cards = new ArrayList<ArrayList>();
players_cards.add(new ArrayList<cards_type>());
When I add the values and print it with this code, it returns the object:
System.out.println(players_cards.get(0).get(0));
OUT: cards_type@ea4a92b
But if I instance a property it returns a mistake like if it wasnt a object:
System.out.println(players_cards.get(0).get(0).color);
OUT: Exception in thread "main" java.lang.Error: Unresolved compilation problem:
color cannot be resolved or is not a field
Thanks for help
答案1
得分: 1
这是你应该修改代码使其正常运行的方式。
初始化ArrayList的方式不正确。你应该始终提供要存储在ArrayList中的数据类型。
ArrayList<ArrayList<cards_type>> players_cards = new ArrayList<ArrayList<cards_type>>();
CardTypes类:
class cards_type {
String color;
String number;
public cards_type(String input_color, String input_number) {
this.color = input_color;
this.number = input_number;
}
}
测试类:
class TestClass {
public static void main(String[] args) {
ArrayList<ArrayList<cards_type>> players_cards = new ArrayList<ArrayList<cards_type>>();
cards_type pc = new cards_type("red", "4");
cards_type pc1 = new cards_type("black", "2");
cards_type pc2 = new cards_type("red", "3");
ArrayList<cards_type> al = new ArrayList<>();
al.add(pc);
al.add(pc1);
al.add(pc2);
players_cards.add(al);
System.out.println(players_cards.get(0).get(0).color);
}
}
英文:
This is how you should modify your code to make it work.
The way you initialized the ArrayList of ArrayList was wrong. You should always provide the type of the data you want to store in the arrayList.
ArrayList<ArrayList<cards_type>> players_cards = new ArrayList<ArrayList<cards_type>>();
CardTypes class:
class cards_type {
String color;
String number;
public cards_type(String input_color, String input_number) {
this.color = input_color;
this.number = input_number;
}
}
Test Class :
Class TestClass{
public static void main(String[] args){
ArrayList<ArrayList<cards_type>> players_cards = new
ArrayList<ArrayList<cards_type>>();
cards_type pc = new cards_type("red","4");
cards_type pc1 = new cards_type("black","2");
cards_type pc2 = new cards_type("red","3");
ArrayList<cards_type> al = new ArrayList<>();
al.add(pc);
al.add(pc1);
al.add(pc2);
players_cards.add(al);
System.out.println(players_cards.get(0).get(0).color);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论