java中如何初始化动态数组?
如果我有一个类需要返回可变维度的字符串数组(并且该维度只能在运行该类的某些方法时确定),那么如何在类的构造函数中声明动态数组?
如果问题不够清楚,
在 php 中我们可以简单地将字符串数组声明为 $my_string_array = array();
并通过 $my_string_array[] = "New value";
添加元素,
那么上面的代码在 java 中相当于什么?
If I have a class that needs to return an array of strings of variable dimension (and that dimension could only be determined upon running some method of the class), how do I declare the dynamic array in my class' constructor?
If the question wasn't clear enough,
in php we could simply declare an array of strings as $my_string_array = array();
and add elements to it by $my_string_array[] = "New value";
What is the above code equivalent then in java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要查看
java.util 包
,特别是ArrayList
类。它具有诸如.add() .remove() .indexof() .contains() .toArray()
等方法。You will want to look into the
java.util package
, specifically theArrayList
class. It has methods such as.add() .remove() .indexof() .contains() .toArray()
, and more.普通 java 数组(即
String[] strings
)无法动态调整大小;当空间不足但仍想向数组添加元素时,您需要创建一个更大的数组并将现有数组复制到其前 n 个位置。幸运的是,有
java.util.List
实现可以为您完成这项工作。java.util.ArrayList
和java.util.Vector
都是使用数组实现的。但是,您真的关心字符串是否恰好存储在数组内部,或者您只需要一个集合来让您不断添加项目而不必担心空间不足?如果是后者,那么您可以选择任何通用
List
实现。大多数情况下,选择是:ArrayList
- 基于基本数组的实现,不同步Vector
- 同步、基于数组的实现LinkedList
- 双向链表实现,可以更快地在列表中间插入项目您希望您的列表有重复的项目吗?如果您的用例不应该存在重复的项目,那么您应该更喜欢
java.util.Set
。保证集合不包含重复的项目。 java.util.HashSet 是一个很好的通用集合实现。回答后续问题
要使用类似于
$my_string_array["property"]
的索引访问字符串,您需要将它们放在Map
,也在java.util
包中。一个好的通用映射实现是HashMap
。创建地图后,
map.put("key", "string")
添加字符串map.get("key")
访问字符串通过它的键。请注意,
java.util.Map
不能包含重复的键。如果使用相同的键连续调用put
,则仅保留最后一次调用中设置的值,较早的值将丢失。但我猜想这也是 PHP 关联数组的行为,所以这并不奇怪。Plain java arrays (ie
String[] strings
) cannot be resized dynamically; when you're out of room but you still want to add elements to your array, you need to create a bigger one and copy the existing array into its firstn
positions.Fortunately, there are
java.util.List
implementations that do this work for you. Bothjava.util.ArrayList
andjava.util.Vector
are implemented using arrays.But then, do you really care if the strings happen to be stored internally in an array, or do you just need a collection that will let you keep adding items without worrying about running out of room? If the latter, then you can pick any of the several general purpose
List
implementations out there. Most of the time the choices are:ArrayList
- basic array based implementation, not synchronizedVector
- synchronized, array based implementationLinkedList
- Doubly linked list implementation, faster for inserting items in the middle of a listDo you expect your list to have duplicate items? If duplicate items should never exist for your use case, then you should prefer a
java.util.Set
. Sets are guaranteed to not contain duplicate items. A good general-purpose set implementation isjava.util.HashSet
.Answer to follow-up question
To access strings using an index similar to
$my_string_array["property"]
, you need to put them in aMap<String, String>
, also in thejava.util
package. A good general-purpose map implementation isHashMap
.Once you've created your map,
map.put("key", "string")
to add stringsmap.get("key")
to access a string by its key.Note that
java.util.Map
cannot contain duplicate keys. If you callput
consecutively with the same key, only the value set in the latest call will remain, the earlier ones will be lost. But I'd guess this is also the behavior for PHP associative arrays, so it shouldn't be a surprise.创建一个
列表
相反。Create a
List
instead.java中没有动态数组,数组的长度是固定的。
类似的结构还有
ArrayList
,它的底层实现了一个真正的数组。查看名称数组列表:)
No dynamic array in java, length of array is fixed.
Similar structure is
ArrayList
, a real array is implemented underlying it.See the name ArrayList :)