This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hello, I have implemented some code to remove dups from an array. I would very much like to advance on to using ArrayList or List but need some pointers on where to do so. In particular, I don't want to have to instantiate an array at a size that may or may not work. Thanks very much.
you could save yourself a lot of time by using a Set instead of a List. A Set automatically takes care that it does not contain any duplicates! Duplicates are determined by the equals() method of an object so the class of the objects you want to store inside a Set should override equals() and hashCode() accordingly. Class Character (which is the wrapper class of "char" you are using) already does this. You should get away with something like this:
Now you should only see character 'a' once in the output ;-)
PS: The Java Collection Framework and other 3rd party libraries offer a lot more features of this kind. So in Java you typically don't have to worry about such low-level details and in general it's better (more performant, robust, secure, ...) to use existing solutions instead of your own ones!
i use collections classes when an array wont work.
if i know the length i use array...if not i use ArrayList(you could also use Vector)
and as was mentioned Set eliminates duplicates
Map is often what you want also
read up about them