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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3656834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 03:14:52  来源:igfitidea点击:

How to initialize a dynamic array in java?

javadynamic-arrays

提问by lock

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";

在 php 中,我们可以简单地将字符串数组声明为 as$my_string_array = array();并向其添加元素$my_string_array[] = "New value";

What is the above code equivalent then in java?

上面的代码在java中是什么?

采纳答案by Tom Neyland

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

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

回答by mik01aj

Create a Listinstead.

创建一个List代替。

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

回答by u290629

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

java中没有动态数组,数组的长度是固定的。类似的结构是ArrayList,在它下面实现了一个真正的数组。请参阅名称数组列表:)

回答by oksayt

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 npositions.

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

Fortunately, there are java.util.Listimplementations that do this work for you. Both java.util.ArrayListand java.util.Vectorare implemented using arrays.

幸运的是,有一些java.util.List实现可以为您完成这项工作。两个java.util.ArrayListjava.util.Vector使用阵列来实现。

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 Listimplementations out there. Most of the time the choices are:

但是,您真的关心字符串是否恰好存储在数组内部,或者您是否只需要一个集合来让您继续添加项目而不必担心空间不足?如果是后者,那么您可以选择几种通用List实现中的任何一种。大多数时候的选择是:

  • 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
  • ArrayList- 基于数组的基本实现,不同步
  • Vector- 同步的,基于数组的实现
  • LinkedList- 双向链表实现,在列表中间插入项目更快

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.

你希望你的列表有重复的项目吗?如果您的用例永远不应该存在重复的项目,那么您应该更喜欢java.util.Set. 集合保证不包含重复项。一个好的通用集合实现是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.utilpackage. A good general-purpose map implementation is HashMap.

要使用类似于 的索引访问字符串$my_string_array["property"],您需要将它们放在Map<String, String>,也在java.util包中。一个好的通用地图实现是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.
  • 使用map.put("key", "string")添加字符串
  • 用于map.get("key")通过键访问字符串。

Note that java.util.Mapcannot contain duplicate keys. If you call putconsecutively 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.

请注意,java.util.Map不能包含重复的键。如果put用同一个键连续调用,则只保留最近调用设置的值,之前调用的值将丢失。但我猜这也是 PHP 关联数组的行为,所以这不足为奇。