11

嗨,我是这个网站的新手,需要帮助我正在开发的程序。我遇到的问题是我似乎无法存储字符串和两个整数(作为坐标)。我查看了其他代码,但没有看到这些值是如何存储的。下面是我一直在使用的代码。代码似乎很好,但是在尝试存储值时我不能放乘整数。谢谢你的时间

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

}
4

5 回答 5

21

java中有一个类叫做Class Point。

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

这与 Java docs API 10 上提供的信息相同:

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

我希望这可以帮助你!

于 2015-02-24T00:58:57.537 回答
9

似乎有几个问题:

  • “旦”是一个String,不是一个Character
  • 大小写在 Java 中很重要(new coords(65,72)应该是new Coords(65,72)
  • 坐标必须是静态的才能在不引用封闭地图类的实例的情况下进行实例化。

这应该有效:

static class Coords {
    ...
}

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

ps:虽然允许map在 a中命名局部变量class map,但出现这种名称冲突并不是一个好主意。在 Java 中,类通常以大写开头,因此您可以重命名您的类 Map。但碰巧 Map 是 Java 中的标准类。因此,将您的课程称为 Main 或 Test 或任何相关的。;-)

于 2012-07-30T11:57:05.717 回答
1

添加到@assylias

  1. 让你inner class 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());
    }
}
于 2012-07-30T12:08:18.840 回答
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 */

}
于 2017-09-26T11:48:27.990 回答
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());              
    }
}
于 2012-07-30T12:14:16.713 回答