#Having an issue with “.size()” function

1 messages · Page 1 of 1 (latest)

thorn marsh
#

Hi, I’m working on refactoring code for an assignment. In this particular spot, I’m modifying a for loop.

Initially, the code was written with array lists for “object_type”, “object_PosX”, “object_PosY”, etc. All values had to be manually inputted.

I’ve reworked it by adding an “object” class that uses the builder method, allowing object types, positions, etc. to be assigned with less code.

I am not very good with lists and for-loops, but I believe that at this spot, the goal is to execute code for each object type, thus getting the number of object types, hence the use of the “.size()” method. However, after rewriting it so that it fits my new code, the size method loses its function. Any tips on how I should be writing this?

atomic sleetBOT
#

<@&987246399047479336> please have a look, thanks.

thorn marsh
#

The image on the left shows my code. The image on the right shows the original code that used array lists. In that instance, “object_type” was an array list.

#

If the .size() method is exclusive to array lists, is there an alternative I can use?

thorn marsh
#

Update: I’ve got the method to work by making an array list to which the objects belong. However, now the for-loop is counting the amount of objects as opposed to the amount of object types. Any tips on making it so it counts the amount of objects types instead?

pearl frigate
#

FYI: java.lang.Object is the base type for objects in Java. You shouldn't create your own type called Object - and you should, in general, avoid creating types that overlap with the public types in the base Java library - doing so will only cause future pain.

For the first screenshots... We don't know what your types are, and therefore don't know the return-type of Object.get_type() (and so can't speculate on the validity of calling .size().

We can't see enough of the code, nor do we have a good explanation of what the code is trying to achieve.

At the least, you can help the helpers by dropping the .java file here (the bot will upload it as a Gist) - screenshots make it much harder for helpers currently on mobile devices to help.