存储X和Y坐标 [英] store X and Y coordinates

查看:135
本文介绍了存储X和Y坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是该站点的新手,需要有关正在使用的程序的帮助。我有的问题是我似乎无法存储字符串和两个整数(作为坐标)。我看过其他代码,但看不到如何存储值。下面是我使用的代码。该代码似乎很好,但是当尝试存储值时,我无法放置乘法整数。谢谢您的时间

Hi im new to this site and need help with a program im working on. the problem im having is that i cant seem to store string and two integers (as the coordinates). i have looked at other code but dont see how the values are stored. below is the code ive been using. the code seems to be fine but when trying to stored the values i cant put multiply integers. thanks for your time

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

}


推荐答案

似乎有几个问题:


  • Dan是 String ,而不是字符

  • 大小写在Java中很重要(新坐标(65,72)应该是新的Coords(65,72)

  • Coords必须是静态的才能实例化而无需引用

  • "Dan" is a String, not a Character
  • case is important in Java (new coords(65,72) should be new Coords(65,72))
  • Coords needs to be static to be instantiated without a reference to an instance the enclosing map class.

这应该起作用:

static class Coords {
    ...
}

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

ps:尽管您可以命名局部变量 map 类映射中的c $ c>,这样的名称冲突不是一个好主意。在Java中,类通常以大写字母开头,因此您可以重命名类Map。但是碰巧Map是Java中的标准类。因此,将您的班级称为Main或Test或其他相关类。 ;-)

ps: although you are allowed to name a local variable map within a class map, it is not a good idea to have such name collision. In Java, classes generally start in upper case, so you could rename your class Map. But it happens that Map is a standard class in Java. So call your class Main or Test or whatever is relevant. ;-)

这篇关于存储X和Y坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆