java 如何将新元素添加到数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7687342/
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
How to add new element into an array
提问by kompotFX
I need to port code from blackberry to android and facing small problem: Example: the bb code is:
我需要将代码从 blackberry 移植到 android 并面临小问题:示例:bb 代码是:
public class MyClass{
private MyObject[] _myObject;
public void addElement(MyObject o){
if (_myObject == null){
_myObject = new MyObject[0];
}
Arrays.add(_myObject, o);
}
}
unfortunately android does not have Arrays.add()
which is part of net.rim.device.api.util.Arrays
(static void add(Object[] array, Object object)
)
不幸的是,android 没有Arrays.add()
哪个是net.rim.device.api.util.Arrays
( static void add(Object[] array, Object object)
) 的一部分
Is there any replacement for android to dynamically extend and append in to simple array so I don't change the rest of my code.
android 是否有任何替代品可以动态扩展并附加到简单数组中,因此我不会更改其余代码。
I tried to write my own utility but it does not work:
我尝试编写自己的实用程序,但它不起作用:
public class Arrays {
public static void add(Object[] array, Object object){
ArrayList<Object> lst = new ArrayList<Object>();
for (Object o : array){
lst.add(o);
}
lst.add(object);
array = lst.toArray();
}
}
.. after I call
.. 在我打电话之后
public void addElement(MyObject o){
if (_myObject == null){
_myObject = new MyObject[0];
}
Arrays.add(_myObject, o);
}
the _myObject
still contain 0 elements.
在_myObject
仍然含有0个元素。
回答by Jon Skeet
Yes, because the _myObject
reference is passed by value. You'd need to use:
是的,因为_myObject
引用是按值传递的。你需要使用:
public static Object[] add(Object[] array, Object object){
ArrayList<Object> lst = new ArrayList<Object>();
for (Object o : array){
lst.add(o);
}
lst.add(object);
return lst.toArray();
}
...
_myObject = Arrays.add(_myObject, o);
However, it would be better to just use an ArrayList<E>
to start with...
但是,最好只使用 anArrayList<E>
开始...
There are two important things to understand here:
这里有两件重要的事情需要理解:
Java alwaysuses pass-by-value
Java总是使用值传递
The value which is passed is either a reference (a way of getting to an object, or null) or a primitive value. That means if you change the value of the parameter, that doesn't get seen by the caller. If you change the value of something within the object the parameter value refers to, that's a different matter:
传递的值要么是一个引用(一种获取对象的方法,要么是 null),要么是一个原始值。这意味着如果您更改了parameter的值,调用者将看不到该值。如果您更改参数值所引用的对象中某些内容的值,那是另一回事:
void doSomething(Person person) {
person.setName("New name"); // This will be visible to the caller
person = new Person(); // This won't
}
Arrays are fixed-size in Java
数组在 Java 中是固定大小的
You can't "add" a value to an array. Once you've created it, the size is fixed. If you want a variable-size collection (which is a very common requirement) you should use an implementation of List<E>
such as ArrayList<E>
.
您不能向数组“添加”一个值。一旦你创建了它,大小就是固定的。如果您想要一个可变大小的集合(这是一个非常常见的要求),您应该使用List<E>
诸如ArrayList<E>
.
回答by Bozho
- commons-langhas
ArrayUtils.add(..)
- guavahas
ObjectArrays.concat(..)
.
- commons-lang有
ArrayUtils.add(..)
- 番石榴有
ObjectArrays.concat(..)
。
Here's the code of ObjectArrays.concat(object, array)
:
这是代码ObjectArrays.concat(object, array)
:
public static <T> T[] concat(@Nullable T element, T[] array) {
T[] result = newArray(array, array.length + 1);
result[0] = element;
System.arraycopy(array, 0, result, 1, array.length);
return result;
}
The apache-commons code is a bit longer.
apache-commons 代码有点长。