I cannot access object properties inside an ArrayList inside another ArrayList, Java.

huangapple go评论58阅读模式
英文:

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&lt;ArrayList&gt;players_cards = new ArrayList&lt;ArrayList&gt;();
players_cards.add(new ArrayList&lt;cards_type&gt;());

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&lt;ArrayList&lt;cards_type&gt;&gt; players_cards = new ArrayList&lt;ArrayList&lt;cards_type&gt;&gt;();

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&lt;ArrayList&lt;cards_type&gt;&gt; players_cards = new 
  ArrayList&lt;ArrayList&lt;cards_type&gt;&gt;();
        		cards_type pc = new cards_type(&quot;red&quot;,&quot;4&quot;);
        		cards_type pc1 = new cards_type(&quot;black&quot;,&quot;2&quot;);
        		cards_type pc2 = new cards_type(&quot;red&quot;,&quot;3&quot;);
        		
        		ArrayList&lt;cards_type&gt; al = new ArrayList&lt;&gt;();
        		al.add(pc);
        		al.add(pc1);
        		al.add(pc2);
        		
        		players_cards.add(al);
        		
        		System.out.println(players_cards.get(0).get(0).color);
        }
    }

huangapple
  • 本文由 发表于 2020年7月31日 15:59:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63187990.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定