We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi! I am just getting started with processing so this is a potentially silly question (first post, HURRAY!).
I have a function that expects an ArrayList full of objects to iterate through, however I would like this function to be able to handle any object within my sketch and I am not quite sure on how to achieve it. When I iterate through the list I need to make a temporary object to hold the values of the iterated object so that is stopping me from being able to feed any object into the list, is that right? I could post some code if it helps clarifying...
I look forward to hear your solutions.
Answers
W/o code, we can only guess what's going on. Here's how far I can help ya:
Hey! Thank you very much. I am pretty new at this and its very likely I am doing something stupid. I have never programmed anything like this before, just trying to figure it out as I go. Here is the code. This function works with an object called Hormone, I would like it to make it work with all objects in all arrayLists in my program. The essential bit is really inside the do while loop
(Hormone myHormone = food.get(counter);)
Hope it is sort of clear, thank you for your help! Meanwhile would you mind explaining a little bit what is happening with your code?
all you use of the Hormone is 'position'. is this going to be true of all the other objects you plan to use? because if it is then there's a mechanism in java for this called interfaces
in short, you define a set of methods (and / or members?) that are common in an interface and then define your classes as implementing that interface... you can then have a list of any mix of these objects and the compiler knows that it can safely call any of the defined methods on any of the objects in that list.
https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html
https://forum.Processing.org/two/discussion/13616/inheritance-in-a-class-with-an-array-loop
can't we look here for that ( @GoToLoop )
https://forum.processing.org/two/discussion/4551/arraylist-with-different-object-types#latest
Hey! I couldn't really find that submit from before, my bad! @koogs: yes, all my objects will have a 'position', it seems that the interface is a bit more achievable for my level of programming for now. I will be looking into the answers and hopefully be able to sort it out. Fantastic help! thank you very much
So... I have been playing with the code and this is what I am at.
I created an interface that holds a method getPosition()
every class in the sketch is then
class whateverClass implements AbsAgent
, and has an implementation of the method getPosition()I am still struggling to understand how to integrate it fully in the sketch though. My function dartThrow() finds a suitable position for placing ann object given certain conditions, and I would like it to work to all classes in my sketch.
so back to my iteration
I thought I could then pass to my function the ArrayList I am working with, like so:
but I keep getting the error cannot convert from object to mySketch.AbsAgent. It is probably me misunderstanding how to use this constructs, but I can't believe I really need to duplicate my code to handle two Arraylists of different types
did you look at my solution?
hey @Chrisir, I did look at your solution and the code I came up with comes a lot from there. However I dont think I should be creating a single ArrayList to deal with all my objects, so I would still like to make it work in different arraylists. is that silly? If I join all the object types in one ArrayList it will end up in a never ending check of objective everywhere in the sketch, maybe I am not thinking about it correctly?
actually I don't understand my own code fully.
but isn't the idea here that the interface encapsules the two different classes so we could say display which in turn means different things for both classes
so you need no checks
yes, that is exactly it, and it is what I am trying to do. my Interface has a getPosition() method that is defined in all my classes, so I thought I could say
but I am not being very lucky, or maybe just really missing the point. any chance you can post some pseudo code mate?
here you see the handling of readX