How come this won't remove?

Discussion in 'Plugin Development' started by bartboy8, Jul 14, 2012.

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

    bartboy8

    I am trying to make an arraylist remove functions. But it won't seem to work... any help?

    Binding Class:
    Code:
    package bm.test;
     
    public class Binding {
     
        int slot;
        String cmd;
        String player;
     
        public Binding(String string, int slot, String cmd){
            this.cmd = cmd;
            this.slot = slot;
            this.player = string;
        }
       
        public String getCmd(){
            return this.cmd;
        }
        public int getSlot(){
            return this.slot;
        }
        public String getName(){
            return this.player;
        }
     
    }
    
    This is what I am using to remove it but it doesn't seem to be working:
    Code:
    binds.remove(new Binding(player.getName(), s, args[0]));
     
  2. Offline

    Darq

    Just because two objects have the same values, doesn't mean they're the same object. You'd have to loop through the objects currently in your list, and then remove the one you're on if the appropriate values match.
     
  3. Hmm, I'm unsure about this but either overwriting toString() or equals() could help you there....
    So, try adding this to your Binding class and re-test:
    Code:
    @Override
    public String toString()
    {
        return player + ";" + cmd + ";" + slot;
    }
    If not, then I dunno... try searching what remove() compares against in the objects.

    EDIT:
    Still.... if that fails, you can always use that to generate a key for a HashMap and use that as a key... I dunno what values are critical tough because I don't know what you're exacly trying to do.
     
  4. Offline

    bartboy8

    Well, i am making a binds plugin. Not with tools though. I am trying to remove 3 values. But those 3 values could be stored multiple times. That is why i am using an arraylist. That is why I cannot do removeAll. Because I want the player to be able to do /unbind [cmd] [slot#]. And unbind different slots.
     
  5. You want to allow users to bind commands to rightclicking with items ? Or on quickbar's slots ? or what ?
     
  6. Offline

    bartboy8

    Well, I have that all done. I just can't get how to remove it.
     
  7. Offline

    kSafin

    If you're making an ArrayList of a custom object and want to be able to use ArrayList methods like .contains() or .remove() you have to:

    1) Implement the equals(Object o) method in the custom object's class.
    2) Implement the hashCode() method in the custom object's class.

    ArrayList will use those two methods to determine if two instances of the class are equal.

    So if you have an ArrayList full of "Binding"s, and you remove a certain "Binding", it'll go through the list and once it compares two "Binding"s with equals() and/or hashCode() and determines they are equal, it'll remove that Binding from the list.
     
    Cowboys1919, bartboy8 and ferrybig like this.
  8. Offline

    bartboy8

    Thanks!

    I have added this in my binding class:
    Code:
        public boolean equals(Object o) {
            if (o instanceof Binding) {
                Binding b = (Binding)o;
                return b.player == this.player && b.slot == this.slot && b.cmd == this.cmd;
            }
            return false;
        }
    But when I try to remove it still doesn't seem to work. Any help?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  9. b.cmd == this.cmd
    strings are never == to each until, unless it is the same string, try b.cmd.equals(this.cmd)
     
  10. Offline

    bartboy8

    Still nothing:
    Code:
        public boolean equals(Object o) {
            if (o instanceof Binding) {
                Binding b = (Binding)o;
                return b.player.equals(this.player) && b.slot == this.slot && b.cmd.equals(this.cmd);
            }
            return false;
        }
     
  11. can you place some debugging code inside the equals, that you see its realy called, and its really comparing those data?
    EDIT: where is your hashCode function?
     
  12. Offline

    bartboy8

    I don't have a hashcode function... I need one? Sorry if I seem a little noobish. This is my first time making a custom object.
     
  13. yes, else you breaking the bindings between equals and hashcode, and may lead to bugs (like you have)
     
  14. Offline

    bartboy8

    So how would I implement a hashCode function... could you tell me what they are. I don't just want code. Because then I don't learn how to do it next time.
     
  15. Offline

    ase34

    try to implement Comparable to class 'Binding'. Maybe this will help.
     
  16. the hashcode is an function that gives an integer, depending on the object state, some quotes form the java api:
    Object.hashcode() (open)
    public int hashCode()
    Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap.
    The general contract of hashCode is:

    Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
    If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
    It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
    As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

    Returns:
    a hash code value for this object.
    See Also:
    equals(java.lang.Object), System.identityHashCode(java.lang.Object)
    Object.equals() (open)
    public boolean equals(Object obj)
    Indicates whether some other object is "equal to" this one.
    The equals method implements an equivalence relation on non-null object references:

    It is reflexive: for any non-null reference value x, x.equals(x) should return true.
    It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
    It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
    It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
    For any non-null reference value x, x.equals(null) should return false.
    The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

    Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

    Parameters:
    obj - the reference object with which to compare.
    Returns:
    true if this object is the same as the obj argument; false otherwise.
    See Also:
    hashCode(), HashMap
    how I implement those 2 functions inside my classes (open)
    Code:java
    1. public final class PlayerDataComparable implements Comparable<PlayerDataComparable>, Cloneable, java.io.Serializable
    2. {
    3. private static final long serialVersionUID = 123456L;
    4. private final String player;
    5. private final int prize;
    6.  
    7. public PlayerDataComparable(String player, int prize)
    8. {
    9. this.player = player;
    10. this.prize = prize;
    11. }
    12.  
    13. @Override
    14. public int compareTo(PlayerDataComparable o)
    15. {
    16. if (o.prize > this.prize)
    17. {
    18. return -1;
    19. }
    20. if (o.prize < this.prize)
    21. {
    22. return 1;
    23. }
    24. return String.CASE_INSENSITIVE_ORDER.compare(this.player, o.player);
    25. }
    26.  
    27. @Override
    28. public boolean equals(Object obj)
    29. {
    30. if (obj == null)
    31. {
    32. return false;
    33. }
    34. if (getClass() != obj.getClass())
    35. {
    36. return false;
    37. }
    38. final PlayerDataComparable other = (PlayerDataComparable) obj;
    39. if ((this.player == null) ? (other.player != null) : !this.player.equals(other.player))
    40. {
    41. return false;
    42. }
    43. if (this.prize != other.prize)
    44. {
    45. return false;
    46. }
    47. return true;
    48. }
    49.  
    50. @Override
    51. public int hashCode()
    52. {
    53. return this.player.hashCode() ^ this.prize;
    54. }
    55.  
    56. @Override
    57. public String toString()
    58. {
    59. return "PlayerDataComparable{"
    60. + "player=" + player
    61. + ", prize=" + prize
    62. + '}';
    63. }/*...*/}
     
    bartboy8 likes this.
  17. Offline

    Firefly

    Can't Eclipse generate a hashCode and equals method for you?
     
  18. Offline

    kSafin

    Even if so, They take about two minutes to implement.
     
  19. Offline

    Firefly

    I realize this, I only said this since the OP of this thread seemed to be having troubles.
     
  20. Offline

    kSafin

    Gotcha
     
Thread Status:
Not open for further replies.

Share This Page