Sunday 15 March 2015

java - Does ArrayList.get() return the same thing as array[]? -


In processing, I have an ArrayList of objects created from a custom class. When I use the .get () function to return one of the objects, it returns the object exactly - but I do not use any variables or methods of the object can do. I get an error message "[variable] can not be solved or there is no area." Is this a bug, or am I doing something wrong?

Here is a sample notification of the values ​​returned from the setup () function.

  // regular array thing [] thinglist1 = new thing [1]; // Arraylight Array Arrayist Chege List 2 = New Arrestist & lt; Thing & gt; (1); // Class Thing Chees = New Thing (12345); // Class Definition Class Thing {int var; Matter (int i) {var = i; Thinglist1 [0] = This; Thinglist2.add (this); }; }; // Drive! Zero setup () {println (thinglist1 [0] == thinglist2.get (0)); // true println (thinglist1 [0] .var); // 12345 println (thinglist2.get (0) .var); // Error: "The word can not be resolved or there is no field"};    

You have messed up some of your mediations.

Change Arraye Chevase 2 = New Arrestist & lt; Thing & gt; (1);

From:

  Arreelist   

Because you have not specified a type that you actually did:

  ArrayList   

So when you retrieve an item from it using get , it should be used instead of your Thing Code> Object was written as code>

Edit to add : The reason is the legacy; When generic was introduced, things were used for previous compatibility. Unfortunately, such situations arise which are confusing Java to a new one.

If you are expecting a compiler warning or error, but Java changes quietly in the non-generic typed ArrayList in the "array list that contains something" ... whatever is (except primitive), because all the objects indirectly object

No comments:

Post a Comment