2012-07-30 210 views
4

嗨即时通讯新的这个网站,需要帮助一个程序即时通讯工作。我的问题是我似乎不能存储字符串和两个整数(作为坐标)。我已经看过其他代码,但没有看到如何存储值。下面是我使用过的代码。代码似乎很好,但当试图存储的值我不能把多个整数。感谢您的时间商店X和Y坐标

import java.util.HashMap; 
public class map { 

    class Coords { 
     int x; 
     int y; 

     public boolean equals(Object o) { 
      Coords c = (Coords) o; 
      return c.x == x && c.y == y; 
     } 

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

     public int hashCode() { 
      return new Integer(x + "0" + y); 
     } 
    } 

    public static void main(String args[]) { 

     HashMap<Coords, Character> map = new HashMap<Coords, Character>(); 
     map.put(new coords(65, 72), "Dan"); 
    } 

} 
+1

为什么你使用这样一个复杂的散列码算法?为什么不使用旧的'x +(某个素数)* y',例如'x + 31 * y'?另外,请注意,您的'equals'实现不考虑'null'值或非法强制转换。 – 2012-07-30 12:05:53

+0

@MattiasBuelens我认为这个hashCode并不复杂,但与素数散列相比,更简单,对吗?它也有明显显示坐标的好处,以0分隔。 – 2017-02-27 14:22:46

+1

@ K_7对于人来说,阅读可能并不复杂,但对于计算机来说,这绝对是比较复杂的(比如:更多的工作)。对于性能至关重要的代码,您希望'hashCode'尽可能少地做(尽管仍然给出一个体面的散列)。 OPs版本进行字符串连接和数字解析,而素数散列只是一个整数加法和一个整数乘法。 – 2017-02-28 20:46:16

回答

6

好像有几个问题:

  • “丹” 是一种String,不是Character
  • 情况下是Java重要(new coords(65,72)应该是new Coords(65,72)
  • Coords需要是静态的,以便在没有引用包含地图类的实例的情况下被实例化。

这应该工作:

static class Coords { 
    ... 
} 

Map<Coords, String> map = new HashMap<Coords, String>(); 
map.put(new Coords(65, 72), "Dan"); 

PS:虽然你被允许到class map内命名一个局部变量map,这不是一个好主意,有这样的名称冲突。在Java中,类通常以大写字母开头,因此您可以重命名类Map。但是恰巧Map是Java中的一个标准类。所以打电话给你的班主或测试或任何相关。 ;-)

+1

另外'Coords'是***内部类***所以只有通过外部类的实例才能创建该类的实例。为了消除这个问题,可以通过添加静态修饰符('static class Coords {/*...*/}')将其转换为***嵌套类***。 – Pshemo 2012-07-30 12:06:16

+0

@Pshemo绝对的,我在测试中将它设为静态,但忘了在我的答案中加入。发现得好。 – assylias 2012-07-30 12:10:42

+0

感谢您的帮助:) – Djchunky123 2012-08-01 17:57:49

1

添加到@assylias

  1. 请以插入新的对象,如您有或new Outer().new Inner()inner classstati℃。
  2. 采取Java Naming Convention

代码的护理,如:

public class XYTest { 
    static class Coords { 
     int x; 
     int y; 

     public boolean equals(Object o) { 
      Coords c = (Coords) o; 
      return c.x == x && c.y == y; 
     } 

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

     public int hashCode() { 
      return new Integer(x + "0" + y); 
     } 
    } 

    public static void main(String args[]) { 

     HashMap<Coords, String> map = new HashMap<Coords, String>(); 

     map.put(new Coords(65, 72), "Dan"); 

     map.put(new Coords(68, 78), "Amn"); 
     map.put(new Coords(675, 89), "Ann"); 

     System.out.println(map.size()); 
    } 
} 
+0

感谢您的回复。只是想知道我如何打印出Coords,因为当我尝试它时再次失败 – Djchunky123 2012-08-01 17:57:26

0

,如果你有你的代码的问题,你可以试试这个,简单的代码来存储串和两个int值成map

class MyCoord{ 
    private int X; 
    private int Y; 

    public MyCoord() { 
     this(0,0); 
    }   
    public MyCoord(int X, int Y) { 
     this.X = X; 
     this.Y = Y; 
    } 
    public int getX() { 
     return X; 
    } 
    public int getY() { 
     return Y; 
    } 
    public void setX(int X) { 
     this.X = X; 
    } 
    public void setY(int Y) { 
     this.Y = Y; 
    } 
} 

//主类开始

public class PointDemo{ 
    public static void main(String[] args) { 

     Map <String,MyCoord> multiplePoints=new HashMap<String, MyCoord>(); 
     multiplePoints.put("point1", new MyCoord(10, 20)); 
     multiplePoints.put("point2", new MyCoord(100, 2000)); 

     MyCoord coord=multiplePoints.get("point1"); 
     System.out.println(coord.getX() +" : "+coord.getY());    
    } 
} 
8

java中有一个叫做Class Point的类。

http://docs.oracle.com/javase/7/docs/api/java/awt/Point.html

表示(x,y)坐标空间中的位置,以整数精度指定的点。

你可以看到和例子此链接:http://www.java2s.com/Tutorial/Java/0261__2D-Graphics/Pointclass.htm

import java.awt.Point; 

class PointSetter { 

    public static void main(String[] arguments) { 
    Point location = new Point(4, 13); 

    System.out.println("Starting location:"); 
    System.out.println("X equals " + location.x); 
    System.out.println("Y equals " + location.y); 

    System.out.println("\nMoving to (7, 6)"); 
    location.x = 7; 
    location.y = 6; 

    System.out.println("\nEnding location:"); 
    System.out.println("X equals " + location.x); 
    System.out.println("Y equals " + location.y); 
    } 
} 

我希望这可以帮助你!

1
package Lecture3; 

import java.util.Scanner; 

public class lecture9 { 

    private int nInleste; 

    public lecture9() {/* 
         * tabell/ // T/*chapter 6 in the books. 
         **/ 
    } 

    public static void main(String[] args) { 
     Scanner inn = new Scanner(System.in); 
     int nInleste = 3; 
     double[] tall = new double[nInleste]; 
     double sum = 0; 
     for (int i = 0; i < nInleste; i++) { 
      System.out.println("Leste en tall!"); 
      tall[i] = inn.nextDouble(); 
      sum += tall[i]; 
     } 
     System.out.println(sum); 
     double snitt = nInleste/nInleste; 
     System.out.println("Gjennomsnittsverdien:" + snitt); 
     for (int i = 0; i < nInleste; i++) { 
      double aavik = tall[i] - snitt; 
      int avvivk = 0; 
      System.out.println(i + 1 + " Tal sitt avvik fra gjennomsnittet " + avvivk); 

     } 
    }/* end of the main methods */ 

}