Tuesday, 15 March 2011

java - what kind of data structure do i need to implement a hashlist for storing coordinates? -


I need to create a status manager class, tell me if the situation is available.

Tried to do this:

  Enter the code here  

Public Class Status Manager {

  hashtable current positions = new hashtable (); Zero occupied post (int x, int y) {this.currentPositionsput (new integer ("4"), new integer ("5"); This.currentPositionsput (new integer ("1"), new integer ("5"); This.currentPositionsput (new integer ("11"), new integer ("3"); This.currentPositionsput (new integer (" 42 "), the new integer (" 55 "); This.currentPositionsput (new integer (" 11 "), new integer (" 53 "); set key = this.currentPositions.keySet (); // Setter.teater = keys.Director (); System.out.println ("The map contains the following teams:"); while (keyIter.hasNext ()) {Object key = keyIter.next (); // Next key Obtain object value = this.currentPositions.get (key); // Get the value for the key System.out.println ("(" + key + "," + value + ");}} Public static zero main (string [] args) {New position manager (.) Captured The post (3, 3);}  

}

Of course this is just an exam, which I try to do I am getting it all the same again, it is used again, the problem is that if I can not duplicate the keys, then what kind of data structure should I use? New? Thanks in advance.

A collection of a set model object that can only be done once a map structure key / The value stores a set of associations. From my studies of my question, I think that a fixed structure makes most sense. Depending on what you want to do with

  // status, you can use the current point. Class status {int x; Int y; The implementation of Hashod () + is equal to ()}}  

You need to implement Hashod () so that the objects can be evenly divided into sets and equal () So that objects can be compared to that. See for more information.

 set  & lt; Status & gt; Status = New Hashet & lt; Status & gt; (); Positions.add (new position (3,4)); Positions.add (new position (5,6)); // and so on  

Be sure to define equals / hashounds (there are lots of links to this)

Now you can check that someone Whether the point is in the set or not using the method such as:

  post cadence (new point (2,1)); // returns false conditions (new point (3,4)); // Return Correct  

No comments:

Post a Comment