具有自己的数据类型的地图(Java)

发布于 2024-09-29 13:42:44 字数 402 浏览 1 评论 0原文

Map<String,MyData>  map = new HashMap<String,MyData>();
...
static class MyData {
  String theString;
  Bitmap theBitmap;
  int theInt;
  ...
}

我怎样才能把数据放到这张地图上???

map.put("xxx", new MyData()); // Does not work

谢谢;)


是的,抱歉我问错了问题;)

我想知道如何在...中写入dada,

例如theString =“aaa”,theInt = 22等...

谢谢

Map<String,MyData>  map = new HashMap<String,MyData>();
...
static class MyData {
  String theString;
  Bitmap theBitmap;
  int theInt;
  ...
}

How can I put data in this map???

map.put("xxx", new MyData()); // Does not work

Thank you ;)


Yes sorry I aked the wrong question ;)

I ment how can I write dada in in ...

like for theString="aaa", theInt=22, etc....

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苏别ゝ 2024-10-06 13:42:44
Map<String,MyData>  map = new HashMap<String,MyData>();
...
class MyData {
  String theString;
  Bitmap theBitmap;
  int theInt;
  ...
}

看看这个是不是更好。

Map<String,MyData>  map = new HashMap<String,MyData>();
...
class MyData {
  String theString;
  Bitmap theBitmap;
  int theInt;
  ...
}

See if this is better.

哎呦我呸! 2024-10-06 13:42:44

当然可以:

import java.util.*;

public class Test {

    static class MyData {
        String theString;
        byte[] theBitmap;
        int theInt;
    }

    public static void main(String... args) {
        Map<String,MyData>  map = new HashMap<String,MyData>();
        map.put("xxx", new MyData());

        System.out.println(map);
    }
}

编译良好,并打印:

{xxx=Test$MyData@3ae48e1b}

Sure it works:

import java.util.*;

public class Test {

    static class MyData {
        String theString;
        byte[] theBitmap;
        int theInt;
    }

    public static void main(String... args) {
        Map<String,MyData>  map = new HashMap<String,MyData>();
        map.put("xxx", new MyData());

        System.out.println(map);
    }
}

This compiles fine, and prints:

{xxx=Test$MyData@3ae48e1b}
野侃 2024-10-06 13:42:44

也许你想做:

MyData someData = new MyData();
someData.theString = "toto";
someData.theString = 1;
map.put("xxx", someData);
someData = new MyData();
someData.theString = "tutu";
someData.theString = 2;
map.put("xxx", someData);

Maybe you want to do :

MyData someData = new MyData();
someData.theString = "toto";
someData.theString = 1;
map.put("xxx", someData);
someData = new MyData();
someData.theString = "tutu";
someData.theString = 2;
map.put("xxx", someData);
梦一生花开无言 2024-10-06 13:42:44

根据您的评论(呃...答案?)我猜测您忘记向 MyData 类添加构造函数。

构造函数应该类似于:

  public MyData(String str, Bitmap bmap, int val)
  {
     // assign the the instnace values and whatever else you neeed to do.
  }

但是,当提出此类问题时,请包含错误消息的文本,以便我们不必猜测给您答案。另外,如果您对问题进行了更改,请将其放在问题中,请勿回复(stackoverflow 并不真正用作讨论板:-)

Based on your comment (er... answer?) I am going to guess that you forgot to add a constructor to the MyData class.

The constructor should be something like:

  public MyData(String str, Bitmap bmap, int val)
  {
     // assign the the instnace values and whatever else you neeed to do.
  }

However, when asking this kind of question please include the text of the error message so that we don't have to guess to give you answers. Also if you have changes to your question put them up in the question don't reply (stackoverflow isn't really used as a discussion board :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文