Help, need to add two integers to a queue and check if they are in there later

Discussion in 'Java' started by Mark, Apr 16, 2005.

  1. Mark

    Mark Guest

    Hi all, I am trying to write a tabu search algorithm for my scheduling
    program, but have hit a problem with the tabu list.

    I am using java 1.5 and creating a queue object as shown...

    Queue tabuList = new LinkedList();

    I need to add a move to the tabu list, a move consists of two integers
    in my program, (I swap two elements in a vector, the ints represent
    the position of the object in the vector)

    I have tried to add the two ints as an array and then run the
    ..contains() on the tabulist, but that will not work, (code shown
    below)

    tabuList.add(new int[](1,2));

    if (tabuList.contains(new int[](1,2)){
    System.out.println("yes");
    }else{
    System.out.println("no");
    }

    This i believe is because the int[] are objects with different
    references, I thought about creating two tabuLists and add one int to
    each list in the same position, but I cannot get the position of the
    int in the tabu list.

    Hope this all makes sense,

    Any help would be fantastic

    Mark
     
    Mark, Apr 16, 2005
    #1
    1. Advertising

  2. Mark

    R.F. Pels Guest

    Mark wrote:

    > tabuList.add(new int[](1,2));


    Uhm, wrap the move in a class:

    public class Move
    implements Comparable
    {

    public Move(int x, int y)
    {
    this.x = x;
    this.y = y;
    }

    public int compareTo(Object o)
    {
    int result = 0; // Assume equality
    if (o instanceof Move)
    {
    Move that = (Move) o;
    result = x - that.x;
    if (result == 0)
    {
    result = y - that.y;
    }
    }
    else
    {
    throw new ClassCastException(o + " is not a Move object");
    }
    return result;
    }

    public int getX()
    {
    return x;
    }

    public void setX(int x)
    {
    this.x = x;
    }

    public int getY()
    {
    return y;
    }

    public void setY(int y)
    {
    this.y = y;
    }

    private int x;
    private int y;
    }

    and use a Set to store them.

    --
    Ruurd
    ..o.
    ...o
    ooo
     
    R.F. Pels, Apr 16, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russell Warren

    Is Queue.Queue.queue.clear() thread-safe?

    Russell Warren, Jun 22, 2006, in forum: Python
    Replies:
    4
    Views:
    697
    Russell Warren
    Jun 27, 2006
  2. Kceiw
    Replies:
    3
    Views:
    1,015
    Jim Langston
    Mar 14, 2006
  3. Frank Millman
    Replies:
    3
    Views:
    355
    Frank Millman
    Feb 9, 2010
  4. Kris
    Replies:
    0
    Views:
    503
  5. bintom
    Replies:
    6
    Views:
    720
    Öö Tiib
    Nov 3, 2012
Loading...

Share This Page