SFML community forums

Bindings - other languages => Java => Topic started by: Helios101 on March 22, 2014, 09:58:17 pm

Title: .
Post by: Helios101 on March 22, 2014, 09:58:17 pm

Title: Re: Java equivalent of the following
Post by: pdinklag on March 25, 2014, 09:34:39 am
Am I right when I assume that you are having a problem with Java, not directly JSFML? I am asking because I am a little confused over what the problem is. I believe you are struggling about which Java collection class to use. There are plenty of them, each is optimized for certain use cases. I suggest reading Oracle's documentations or some tutorial on Java collections.

In your case, if you need a dynamic array and you want to access elements by their indices, use the ArrayList. It uses an array internally and is therefore optimized for direct index access. The Vector class does practically the same, but it's a thread-safe version. Unless you need thread-safety, there's no need for you to use the Vector class.

Concerning the decision between ArrayList and Vector, here's a nice little face-off:
Title: Re: Java equivalent of the following
Post by: Nexus on March 25, 2014, 09:41:11 pm
My problem is I don't know how to access the abstract run method from the ArrayList object, similar to the way I would go about accessing a virtual function from a vector object in C++
That description is misleading: neither in C++ nor Java, the run method is a part of the container. In fact, containers and collections have absolutely nothing to do with the way you call a method on an object.

I have tried doing the same technique in Java, but I've had no luck.
Java methods are always called with the . operator, there is no explicit dereferencing.

Instead of trying C++ things blindly in Java, you should learn the language properly. There are quite good tutorials on Oracle's homepage, but you could also read a book. To learn how to use ArrayList, have a look at the official Java documentation.