存储X和Y坐标

12

大家好,我是这个网站的新手,需要帮助解决我正在开发的一个程序的问题。我遇到的问题是似乎无法存储字符串和两个整数(作为坐标)。我查看了其他代码,但不知道如何存储值。下面是我一直在使用的代码。代码看起来没问题,但当我尝试存储这些值时,我无法同时输入多个整数。谢谢你们的时间。

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 值或非法转换。 - Mattias Buelens
@MattiasBuelens 我认为这个hashCode并不复杂,相比于质数哈希而言,它更简单,对吗?它还有一个好处,就是明确显示由0分隔的坐标。 - Kartik Chugh
1
@K_7 或许对人类来说更易读,但对计算机来说却更复杂(即:更多工作)。对于性能关键的代码,您希望 hashCode 尽可能少地执行操作(同时仍然提供合理的哈希值)。原帖中的版本执行了字符串连接和数字解析,而质数哈希只需要一个整数加法和一个整数乘法。 - Mattias Buelens
5个回答

22

Java中有一个名为Class Point的类。

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

https://docs.oracle.com/javase/10/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);
  }
}

我希望这可以帮助到您!


9

似乎有几个问题:

  • “Dan”是一个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:虽然你可以在类map内部命名一个本地变量为map,但是这样的名称冲突并不好。在Java中,类通常以大写字母开头,所以你可以将你的类重命名为Map。但是恰巧在Java中Map是一个标准类。因此,请将你的类命名为Main、Test或者其他相关名称。;-)


1
另外,Coords 是一个内部类,因此创建该类的实例的唯一方法是通过外部类的实例。为了解决这个问题,将其转换为嵌套类可能是一个好主意,只需添加静态修饰符(static class Coords{/*...*/})即可。 - Pshemo
@Pshemo 绝对没错,我在我的测试中将它设为静态但忘记在答案中包括了。你很敏锐。 - assylias

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 */

}

1

回复 @assylias

  1. 将你的 内部类 设为 static,这样就可以像你所做的那样插入新对象,例如 new Outer().new Inner()
  2. 注意Java 命名规范

代码示例:

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());
    }
}

谢谢回复。我只是想知道如何打印出坐标,因为当我尝试时它不起作用。再次感谢。 - Djchunky123

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());              
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接