如何动态地将项添加到Java数组?

kam*_*lot 80 java arrays dynamic-arrays

在PHP中,您可以通过以下方式动态向元数添加元素:

$x = new Array();
$x[] = 1;
$x[] = 2;
Run Code Online (Sandbox Code Playgroud)

在此之后,$x将是这样的数组:{1,2}.

有没有办法在Java中做类似的事情?

cor*_*iKa 108

查看java.util.LinkedList或java.util.ArrayList

List<Integer> x = new ArrayList<Integer>();
x.add(1);
x.add(2);
Run Code Online (Sandbox Code Playgroud)

  • 这是一个完美的答案,除了一件.通常在Java中我们只使用List而不是原始数组,但对于实际需要原始数组的实例(如`int []`),只需在List上使用`toArray()`方法.使用上面的代码,但使用`x.toArray()`来获取原始数组. (21认同)
  • 哦,所有这些关于如何使用原始数组的评论,如果只是那么容易:( (10认同)

Paŭ*_*ann 64

Java中的数组具有固定的大小,因此您不能像在PHP中那样"在末尾添加内容".

有点类似于PHP的行为是这样的:

int[] addElement(int[] org, int added) {
    int[] result = Arrays.copyOf(org, org.length +1);
    result[org.length] = added;
    return result;
}
Run Code Online (Sandbox Code Playgroud)

然后你可以写:

x = new int[0];
x = addElement(x, 1);
x = addElement(x, 2);

System.out.println(Arrays.toString(x));
Run Code Online (Sandbox Code Playgroud)

但是这种方案对于较大的阵列来说非常低效,因为它每次都会复制整个阵列.(事实上​​它并不完全等同于PHP,因为你的旧数组保持不变).

PHP数组实际上与添加了"max key"的Java HashMap完全相同,因此它将知道下一个使用哪个键,以及奇怪的迭代顺序(以及Integer键和某些字符串之间的奇怪等价关系).但是对于简单的索引集合,最好在Java中使用List,就像其他的回答者一样.

如果List由于在Integer中包装每个int的开销而要避免使用,请考虑对基本类型使用集合的重新实现,这些类型在内部使用数组,但只有在内部数组已满时才会对每次更改执行复制(就像ArrayList).(一个快速搜索的示例是这个IntList类.)

番石榴含有制造这种包装方法Ints.asList,Longs.asList等等.

  • @Mihail:实际上,我通常不建议这样做,因为每次添加元素时都会创建一个新数组并复制整个内容.(这取决于你的使用案例 - 如果你很少添加/删除任何东西,并经常迭代/搜索,它可能没问题.) (2认同)

mis*_*ist 19

Apache Commons有一个ArrayUtils实现,可以在新数组的末尾添加一个元素:

/** Copies the given array and adds the given element at the end of the new array. */
public static <T> T[] add(T[] array, T element)
Run Code Online (Sandbox Code Playgroud)

  • 在较新的ArrayUtils中,它是:ArrayUtils.appendElement(String.class,origArray,“ new element”) (2认同)

Sal*_*ara 12

我经常在网上看到这个问题,在我看来,许多声誉很高的人没有正确回答这些问题.所以我想在这里表达我自己的答案.

首先,我们应该考虑和之间存在差异.arrayarraylist

该问题要求向数组添加元素,而不是ArrayList


答案很简单.它可以分3个步骤完成.

  1. 将数组转换为arraylist
  2. 将元素添加到arrayList
  3. 将新的arrayList转换回数组

这是它的简单图片 在此输入图像描述

最后这里是代码:

步骤1:

public List<String> convertArrayToList(String[] array){
        List<String> stringList = new ArrayList<String>(Arrays.asList(array));
        return stringList;
    }
Run Code Online (Sandbox Code Playgroud)

第2步:

public  List<String> addToList(String element,List<String> list){

            list.add(element);
            return list;
    }
Run Code Online (Sandbox Code Playgroud)

第3步:

public String[] convertListToArray(List<String> list){
           String[] ins = (String[])list.toArray(new String[list.size()]);
           return ins;
    } 
Run Code Online (Sandbox Code Playgroud)

第4步

public String[] addNewItemToArray(String element,String [] array){
        List<String> list = convertArrayToList(array);
        list= addToList(element,list);
        return  convertListToArray(list);
}
Run Code Online (Sandbox Code Playgroud)


Ian*_*las 11

您可以使用a ArrayList然后使用该toArray()方法.但是根据你在做什么,你甚至可能根本不需要阵列.看看是否Lists更符合你的需求.

请参阅:Java列表教程


Hov*_*els 5

您可能希望为此使用ArrayList - 对于动态大小的数组(如结构).