Java 关联数组
如何像在 PHP 中一样在 Java 中创建和获取关联数组?
例如:
$arr[0]['name'] = 'demo';
$arr[0]['fname'] = 'fdemo';
$arr[1]['name'] = 'test';
$arr[1]['fname'] = 'fname';
How can I create and fetch associative arrays in Java like I can in PHP?
For example:
$arr[0]['name'] = 'demo';
$arr[0]['fname'] = 'fdemo';
$arr[1]['name'] = 'test';
$arr[1]['fname'] = 'fname';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
Java 不支持关联数组,但是使用
Map
可以轻松实现这一点。例如,对于您的示例来说更准确(因为您可以将 String 替换为满足您需求的任何对象)是声明:
更多信息请参阅官方文档
Java doesn't support associative arrays, however this could easily be achieved using a
Map
. E.g.,Even more accurate to your example (since you can replace String with any object that meet your needs) would be to declare:
See the official documentation for more information
Java 没有像 PHP 那样的关联数组。
对于您正在做的事情有多种解决方案,例如使用地图,但这取决于您想要如何查找信息。您可以轻松编写一个类来保存所有信息并将它们的实例存储在 ArrayList 中。
然后...
这样您就可以像...一样访问它们
Java doesn't have associative arrays like PHP does.
There are various solutions for what you are doing, such as using a Map, but it depends on how you want to look up the information. You can easily write a class that holds all your information and store instances of them in an
ArrayList
.And then...
So you can access them like...
您可以通过地图来完成此任务。但当
你开始使用 Java 时,我相信你会发现,如果你创建一个代表你的数据的类/模型将是你最好的选择。我会做
You can accomplish this via Maps. Something like
But as you start using Java I am sure you will find that if you create a class/model that represents your data will be your best options. I would do
查看 Map 接口,以及具体类 HashMap。
创建映射:
添加键值对:
检索与键关联的值:
当然,您可以创建映射数组,因为它似乎是您想要的:
Look at the Map interface, and at the concrete class HashMap.
To create a Map:
To add a key-value pair:
To retrieve the value associated with a key:
And sure, you may create an array of Maps, as it seems to be what you want:
Java 中没有关联数组这样的东西。它最接近的是
Map
,它是强类型的,但语法/API 不太优雅。这是根据您的示例可以获得的最接近的结果:
There is no such thing as associative array in Java. Its closest relative is a
Map
, which is strongly typed, however has less elegant syntax/API.This is the closest you can get based on your example:
好吧,我也在寻找关联数组,并发现地图列表是最佳解决方案。
Well i also was in search of Associative array and found the List of maps as the best solution.
Perl 哈希的 Java 等效项
Java equivalent of Perl's hash
Java 没有关联数组,您可以获得的最接近的是 Map界面
这是该页面的示例。
如果运行:
你会得到:
Java doesn't have associative arrays, the closest thing you can get is the Map interface
Here's a sample from that page.
If run with:
You'll get:
使用ArrayList <地图<字符串,字符串> >
这里是一个代码示例:
输出:
产品:[{id=0001,name=prod1},{id=0002,name=prod2}]
Use ArrayList < Map < String, String > >
Here a code sample :
Output :
products : [{id=0001, name=prod1}, {id=0002, name=prod2}]
Java 中的关联数组与 PHP 中的关联数组:
更多信息,请参阅 SoftHashMap 类:https://shiro.apache.org/static/1.2.2/apidocs/org/apache/shiro/util/SoftHashMap.html
Associative arrays in Java like in PHP :
More info, see Class SoftHashMap : https://shiro.apache.org/static/1.2.2/apidocs/org/apache/shiro/util/SoftHashMap.html
是的,这需要迭代来按键搜索值,但如果您需要所有这些,这将是最佳选择。
Yes, this require iteration for searchting value by key, but if you need all of them, this will be the best choice.
在 JDK 1.5 (http://tinyurl.com/3m2lxju) 中甚至有一条注释:“注意:这个类已过时。新的实现应该实现 Map 接口,而不是扩展这个类。”
问候,N.
In JDK 1.5 (http://tinyurl.com/3m2lxju) there is even a note: "NOTE: This class is obsolete. New implementations should implement the Map interface, rather than extending this class."
Regards, N.
实际上Java确实支持关联数组,它们被称为字典!
Actually Java does support associative arrays they are called dictionaries!
仔细考虑一下,我想抛弃元组作为处理这个问题的更通用的方法。虽然元组不是 Java 原生的,但我使用 Javatuples 为我提供其他语言中存在的相同功能。如何处理所提出的问题的一个例子是
我喜欢这种方法,因为它可以使用 api 提供的类和方法扩展到三元组和其他更高顺序的分组。
Thinking more about it, I would like to throw out tuples as a more general-purpose way of dealing with this problem. While tuples are not native to Java, I use Javatuples to provide me the same functionality which would exist in other languages. An example of how to deal with the question asked is
I like this approach because it can be extended to triples and other higher ordered groupings with api provided classes and methods.
关于 PHP 评论“不,PHP 不会喜欢它”。实际上,除非您设置一些非常严格的(对于 PHP)异常/错误级别,否则 PHP 会继续运行(甚至可能不会)。
默认情况下会发生的情况是,访问不存在的变量/越界数组元素会“取消设置”您要分配的值。不,那不是空的。据我了解,PHP 具有 Perl/C 血统。因此,有:未设置和不存在的变量、已设置但为 NULL 的值、布尔 False 值,以及标准语言所具有的其他所有内容。您必须单独测试这些,或者选择内置函数/语法的正确评估。
Regarding the PHP comment 'No, PHP wouldn't like it'. Actually, PHP would keep on chugging unless you set some very restrictive (for PHP) exception/error levels, (and maybe not even then).
What WILL happen by default is that an access to a non existing variable/out of bounds array element 'unsets' your value that you're assigning to. NO, that is NOT null. PHP has a Perl/C lineage, from what I understand. So there are: unset and non existing variables, values which ARE set but are NULL, Boolean False values, then everything else that standard langauges have. You have to test for those separately, OR choose the RIGHT evaluation built in function/syntax.