Solved Same Object in ArrayList multiple times

Discussion in 'Plugin Development' started by Irantwomiles, Oct 11, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    Irantwomiles

    This is more of a java question than a Bukkit one. I know it's possible to have the same object or string or any form of data you are using multiple times in a arraylist, but my question is, is it bad? is it one of those bad coding practices, because the plugin I'm writing relies on it. If it is bad what alternatives do I have to get the same result?
     
  2. Offline

    Firestar311

    This is bad practice. May I ask why do you need the same object multiple times in an ArrayList?
    You can use a HashMap or multiple ArrayLists
     
  3. Offline

    Irantwomiles

    I was planning on creating a plugin where different people have different percetages of winning an item and I was going to make it so a person was in a list multiple times and by using a random number generator I could pick someone ou t of the list. In theory the higher percentage the more your name will be on that list and the higher chance of your name being chosen by the random number generator.
     
  4. Offline

    Lordloss

    I dont think it is bad practice if its the simplest and most clear way for you of doing something. If you really need to never have a value twice, you should use a set. Imo there is a reason duplicates are not forbidden for lists.
     
  5. Offline

    I Al Istannen

    @Lordloss @Irantwomiles
    I have made a random picker for one project here. If you implement WeightedObject, you should be able to easily adjust it to your needs.

    It uses the statistic weight, and an algorithm described on a StackOverflow page I have lost since then :/ So I can't link you to how it works sadly. But it seems to be the most common and simple implementation. You can find a similar one here.
    Maybe you want to modify it to be able to add an object with the weight to not need to implement the interface though.

    I would make sure the sum of all probabilites is a multiple of ten, but it will work either way. The percentages may be a bit off though, if you don't.
     
  6. Offline

    Irantwomiles

    Thanks, but I saw that it cant contain the same object more than once. Also, this is pretty childish, but CumSum :p

    Thats what I was thinking. My logic was that there wouldn't be a List that can contain the same object more than once if it wasn't meant to be a feature, or everything would be like a set.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  7. Offline

    I Al Istannen

    @Irantwomiles
    Why would it contain one object more than one time? If you want it to occur more ioften, just increase the weight.
    If you want to add it twice, double it's weight instead.

    And if you really want, just remove the contains check. Won't break anything, but remove will then remove every instance of the object (default List behaviour).

    "cum" is based on latin, meaning "together (with)". Blame the romans xD
     
  8. Offline

    mythbusterma

    @Irantwomiles

    The reason a list allows duplicates is quite simple. Checking for duplicates is quite expensive. It is usually considered a feature that a Set doesn't allow duplicates, and just a trade-off for a list, so that it is lighter on CPU and memory.
     
  9. Offline

    Irantwomiles

    So would there be a problem if I had the same thing multiple times? Like would it cause any memory leaks or catch my computer on fire?
     
  10. Offline

    mythbusterma

    @Irantwomiles

    Absolutely not. In my opinion, the best way to do this would be a List of "tokens" or "tickets," which itself is an object that contains a reference to the player that the token belongs to. Then, just pick a random one out of the List. The reason that this

    Alternatively, the weighted percentages is fine too.
     
    I Al Istannen likes this.
  11. Offline

    Irantwomiles

    ok cool. thanks everyone that answered on this thread!
     
    I Al Istannen likes this.
Thread Status:
Not open for further replies.

Share This Page