4 Replies Latest reply: Apr 12, 2008 7:26 PM by 807601 RSS

    ArrayList not staying in order

    807601
      Hi all,

      I have an arraylist of points scores and i want to be able to 'set' the value of the element with the lowest points score to something new. However my ArrayList doesn't stay in order so I can never tell where the correct element will be. Is there any way of having the elements in a fixed order?

      thanks
        • 1. Re: ArrayList not staying in order
          807601
          Consider using SortedSet rather than ArrayList to hold the elements.
          • 2. Re: ArrayList not staying in order
            807601
            ok thanks, but then there seems to be no method to manually change an element of a SortedSet...
            • 3. Re: ArrayList not staying in order
              807601
              You don't say what manner of beast the elements in your collection are.

              Perhaps they are mutable - in other words their score can change while their identity stays the same. In that case the elements themselves should provide a method that allows the score to change. Note, the elements of the set don't change in this case, their scores do. And note, too that the set doesn't keep its elements sorted: you have to create a new set and add all of the elements to it to resort it if you want this behaviour.

              Here's an example:
              import java.util.SortedSet;
              import java.util.TreeSet;
              
              public class SortEg {
                  public static void main(String args[]) {
                      SortedSet<Person> people = new TreeSet<Person>();
                      people.add(new Person("Alf", 5));
                      people.add(new Person("Beth", -1));
                      people.add(new Person("Gem", 666));
                      people.add(new Person("Beth2", -1));
                        
                      System.out.println(people);
                      people = mark1030(people);
                      System.out.println(people);
                  }
                   
                      // swaps first<->last
                  static SortedSet<Person> mark1030(SortedSet<Person> people) {
                      Person first = people.first(); 
                      Person last = people.last(); 
                      int min = first.getValue();
                      int max = last.getValue();
                        
                      first.setValue(max + 1);
                      last.setValue(min - 1);
                        
                      TreeSet<Person> ret = new TreeSet<Person>();
                      for(Person p :people) {
                          ret.add(p);
                      }
                      return ret;
                  }
              }
              
              class Person implements Comparable<Person> {
                  private String name;
                  private int value;
                   
                  public Person(String name, int value) {
                      this.name = name;
                      this.value = value;
                  }
                   
                  @Override
                  public String toString() {
                      return String.format("Person[%s: %d]", name, value);
                  }
                   
                  public String getName() {return name;}
                   
                  public int getValue() {return value;}
                  public void setValue(int v) {value = v;}
              
                  public int compareTo(Person o) {
                      int ret = value - o.value;
                      if(ret != 0) return ret;
                      return hashCode() - o.hashCode(); 
                  }
              }
              On the other hand, perhaps the elements are immutable (can't change value): eg they could be the scores themselves as Integers, say. In that case you remove and discard the element, replacing it with another of the desired value. Again if you wish to keep the collection sorted, create a new set and add the new element along with the others.

              (If the collection is very large you could write your own class that does the resorting more efficently.)
              • 4. Re: ArrayList not staying in order
                807601
                On second thoughts, that's dumb. The remove-update-replace strategy works for the mutable case as well.

                Here's the RSV:
                    // swaps first<->last
                static void mark1030(SortedSet<Person> people) {
                    Person first = people.first();
                    Person last = people.last(); 
                    int min = first.getValue();
                    int max = last.getValue();
                          
                    people.remove(first);
                    people.remove(last);
                          
                    first.setValue(max + 1);
                    last.setValue(min - 1);
                          
                    people.add(first);
                    people.add(last);
                }