如何创建Java地图固定长度的数组值吗? [英] How to create Java Map of fixed-length array values?

查看:106
本文介绍了如何创建Java地图固定长度的数组值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个计划,需要存储几个简单的整型值和每一个字符串键关联。目前我初始化这个成员,像这样:

I have a program which needs to store a couple of simple int values and associate each with a String key. Currently I initialize this member like so:

private Map<String, int[]> imageSizes = new HashMap<String, int[]>();

然后追加给它的东西,如 imageSizes.put(一些关键,新的INT [] {100,200});

我的问题是 - 有没有办法让这些值的固定长度是多少?我将只需要在每个2元。如果我尝试给阵列中的成员定义的长度Java不喜欢的语法。

My question is this - is there a way to give these values a fixed length? I will only ever need 2 elements in each. Java doesn't like the syntax if I try to give the arrays a length in the member definition.

此外,是否有限制在这种情况下数组长度任何好处,还是我只是被过分热心的在我的优化?

Furthermore, is there any benefit to restricting the array length in this case, or am I just being overzealous in my optimisation?

推荐答案

新INT [] 仅在数组中的元素按照括号和编译器建设的法律可以算多少。在地图上的存储器用于值的量为基准(32或64位),只是大小不管阵列的本身的多大。所以定影阵列的大小将不会由地图改变的存储器使用量的 的。在C语言中,你可以声明一个指针,然后分配更多或更少的内存给它(或忘记分配任何和崩溃); Java的内存管理你。

new int[] is only legal in a construction where the array elements follow in braces and the compiler can count how many. In the Map, the amount of memory used for the values is just the size of the reference (32 or 64 bits), no matter how large the array is itself. So fixing the size of the array won't change the amount of memory used by the map. In C, you could declare a pointer and then allocate more or less memory to it (or forget to allocate any and crash); Java manages memory for you.

这篇关于如何创建Java地图固定长度的数组值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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