如何动态地将项目添加到 Java 数组中?
在 PHP 中,您可以通过以下方式动态地将元素添加到数组中:
$x = new Array();
$x[] = 1;
$x[] = 2;
此后,$x
将是一个如下所示的数组:{1,2}
。
有没有办法在Java中做类似的事情?
In PHP, you can dynamically add elements to arrays by the following:
$x = new Array();
$x[] = 1;
$x[] = 2;
After this, $x
would be an array like this: {1,2}
.
Is there a way to do something similar in Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
查看 java.util.LinkedList 或 java.util.ArrayList
Look at java.util.LinkedList or java.util.ArrayList
Java 中的数组具有固定大小,因此您无法像 PHP 中那样“在末尾添加一些内容”。
与 PHP 的行为有点相似的是:
然后你可以这样写:
但是这个方案对于较大的数组来说效率非常低,因为它每次都会复制整个数组。 (事实上,它并不完全等同于 PHP,因为你的旧数组保持不变)。
PHP 数组实际上与添加了“最大键”的 Java HashMap 完全相同,因此它会知道接下来使用哪个键,以及奇怪的迭代顺序(以及整数键和某些字符串之间奇怪的等价关系)。但对于简单的索引集合,最好使用 Java 中的 List,就像其他回答者建议的那样。
如果您想避免使用
List
因为将每个 int 包装在 Integer 中的开销,请考虑使用原始类型集合的重新实现,它在内部使用数组,但不会在每次更改时进行复制,仅当内部数组已满时(就像 ArrayList 一样)。 (在谷歌上快速搜索到的一个示例是这个 IntList 类。 )Guava 在
Ints 中包含创建此类包装器的方法 .asList
、Longs.asList
等。Arrays in Java have a fixed size, so you can't "add something at the end" as you could do in PHP.
A bit similar to the PHP behaviour is this:
Then you can write:
But this scheme is horribly inefficient for larger arrays, as it makes a copy of the whole array each time. (And it is in fact not completely equivalent to PHP, since your old arrays stays the same).
The PHP arrays are in fact quite the same as a Java HashMap with an added "max key", so it would know which key to use next, and a strange iteration order (and a strange equivalence relation between Integer keys and some Strings). But for simple indexed collections, better use a List in Java, like the other answerers proposed.
If you want to avoid using
List
because of the overhead of wrapping every int in an Integer, consider using reimplementations of collections for primitive types, which use arrays internally, but will not do a copy on every change, only when the internal array is full (just like ArrayList). (One quickly googled example is this IntList class.)Guava contains methods creating such wrappers in
Ints.asList
,Longs.asList
, etc.Apache Commons 有一个 ArrayUtils 实现在新数组末尾添加元素:
Apache Commons has an ArrayUtils implementation to add an element at the end of the new array:
我在网上经常看到这个问题,在我看来,许多享有盛誉的人都没有正确回答这些问题。所以我想在这里表达我自己的答案。
首先我们应该考虑有一个
array
和arraylist
之间的区别。问题要求添加一个元素到数组,而不是ArrayList
答案非常简单。分 3 步即可完成。
这里是它的简单图片
最后是代码:
第 1 步:
第 2 步:
第 3 步:
第 4 步< /强>
I have seen this question very often in the web and in my opinion, many people with high reputation did not answer these questions properly. So I would like to express my own answer here.
First we should consider there is a difference between
array
andarraylist
.The question asks for adding an element to an array, and not ArrayList
The answer is quite simple. It can be done in 3 steps.
Here is the simple picture of it
And finally here is the code:
Step 1:
Step 2:
Step 3:
Step 4
您可以使用
ArrayList
,然后使用toArray()
方法。但根据您正在做什么,您甚至可能根本不需要数组。查看Lists
是否更符合您的需求。请参阅:Java 列表教程
You can use an
ArrayList
and then use thetoArray()
method. But depending on what you are doing, you might not even need an array at all. Look into seeing ifLists
are more what you want.See: Java List Tutorial
您可能想为此使用 ArrayList - 对于动态大小的数组之类的结构。
You probably want to use an ArrayList for this -- for a dynamically sized array like structure.
您可以使用 JAVA 中的集合框架动态地将元素添加到数组中。集合框架不适用于原始数据类型。
这个集合框架将在“java.util.*”包中提供
例如,如果您使用ArrayList,
请为其创建一个对象,然后添加元素数量(任何类型,如字符串,整数...等)
现在您添加了3个元素到数组。
如果您想
再次删除任何添加的元素如果您想添加任何元素
那么数组大小会动态增加/减少。
You can dynamically add elements to an array using Collection Frameworks in JAVA. collection Framework doesn't work on primitive data types.
This Collection framework will be available in "java.util.*" package
For example if you use ArrayList,
Create an object to it and then add number of elements (any type like String, Integer ...etc)
Now you were added 3 elements to an array.
if you want to remove any of added elements
again if you want to add any element
So the array size is incresing / decreasing dynamically..
使用 ArrayList 或处理数组来自动增加数组大小。
Use an ArrayList or juggle to arrays to auto increment the array size.
记录您在原始数组中的位置
keep a count of where you are in the primitive array
这是在java中添加到数组的简单方法。我使用第二个数组来存储原始数组,然后向其中添加一个元素。之后我将该数组传递回原始数组。
This is a simple way to add to an array in java. I used a second array to store my original array, and then added one more element to it. After that I passed that array back to the original one.
在 Java 中,数组的大小是固定的,但您可以使用索引和 for 循环将元素动态添加到固定大小的数组中。请在下面找到示例。
In Java size of array is fixed , but you can add elements dynamically to a fixed sized array using its index and for loop. Please find example below.