java中如何初始化动态数组?

发布于 2024-09-17 11:40:23 字数 248 浏览 0 评论 0原文

如果我有一个类需要返回可变维度的字符串数组(并且该维度只能在运行该类的某些方法时确定),那么如何在类的构造函数中声明动态数组?

如果问题不够清楚,

在 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 技术交流群。

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

发布评论

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

评论(4

星星的轨迹 2024-09-24 11:40:23

您需要查看java.util 包,特别是ArrayList 类。它具有诸如 .add() .remove() .indexof() .contains() .toArray() 等方法。

You will want to look into the java.util package, specifically the ArrayList class. It has methods such as .add() .remove() .indexof() .contains() .toArray(), and more.

执笏见 2024-09-24 11:40:23

普通 java 数组(即 String[] strings)无法动态调整大小;当空间不足但仍想向数组添加元素时,您需要创建一个更大的数组并将现有数组复制到其前 n 个位置。

幸运的是,有 java.util.List 实现可以为您完成这项工作。 java.util.ArrayListjava.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 first n positions.

Fortunately, there are java.util.List implementations that do this work for you. Both java.util.ArrayList and java.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 synchronized
  • Vector - synchronized, array based implementation
  • LinkedList - Doubly linked list implementation, faster for inserting items in the middle of a list

Do 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 is java.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 a Map<String, String>, also in the java.util package. A good general-purpose map implementation is HashMap.

Once you've created your map,

  • Use map.put("key", "string") to add strings
  • Use map.get("key") to access a string by its key.

Note that java.util.Map cannot contain duplicate keys. If you call put 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.

方觉久 2024-09-24 11:40:23

创建一个 列表 相反。

List<String> l = new LinkedList<String>();
l.add("foo");
l.add("bar");

Create a List instead.

List<String> l = new LinkedList<String>();
l.add("foo");
l.add("bar");
z祗昰~ 2024-09-24 11:40:23

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 :)

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