添加到已设置/已填充的数组

时间:2022-02-08 06:56:12

Is it possible to do this? I want to be able to give the user the option to add another element to the array which is set to the length 5 and has already been filled. I believe this will increase the array length by 1? Also, please know that I know how to do this in ArrayList. I want to be able to do this in a normal array.

是否有可能做到这一点?我希望能够为用户提供将另一个元素添加到数组的选项,该数组设置为长度为5并且已经填充。我相信这会使数组长度增加1?另外,请知道我知道如何在ArrayList中执行此操作。我希望能够在普通数组中执行此操作。

I heard that Arrays.copyof() can help do this but I don't understand how?

我听说Arrays.copyof()可以帮助做到这一点,但我不明白怎么做?

4 个解决方案

#1


1  

You can't add one more element if your array is filled.

如果数组已填满,则无法再添加一个元素。

You need to build a bigger array and copy the values from the old array to the new one. That's where Arrays.copyOf comes in handy.

您需要构建一个更大的数组并将值从旧数组复制到新数组。这就是Arrays.copyOf派上用场的地方。

For performance reason, it would be better to add more than 1 empty cell each time you rebuild a new array. But basically, you will build your own implementation of ArrayList.

出于性能原因,每次重建新阵列时最好添加多个空单元格。但基本上,您将构建自己的ArrayList实现。

#2


0  

In an ArrayList you can just add another value without having to do anything. Internally, the ArrayList will create a new, larger array, copy the old one into it, and add the value to it.

在ArrayList中,您只需添加另一个值,而无需执行任何操作。在内部,ArrayList将创建一个新的更大的数组,将旧数组复制到其中,并将值添加到其中。

If you want to do this with an array, you will need to do this work yourself. As you are thinking, Arrays.copyOf() is a simple way to do that. For instance:

如果你想用数组做这个,你需要自己做这项工作。正如您所想,Arrays.copyOf()是一种简单的方法。例如:

    int[] a = {1,2,3,4,5};
    System.out.println(a.length); // this will be 5
    System.out.println(Arrays.toString(a)); // this will be [1, 2, 3, 4, 5]

    int[] b = Arrays.copyOf(a, 10);
    System.out.println(b.length); // this will be 10, half empty
    System.out.println(Arrays.toString(b)); // this will be [1, 2, 3, 4, 5, 0, 0, 0, 0, 0]

#3


0  

import java.util.Arrays;

int[] myArray = new int[]{1,2,3,4,5}; //The array of five

int[] myLongerArray = Arrays.copyOf(myArray, myArray.length + 1); //copy the original array into a larger one

myLongerArray[myLongerArray.length-1] = userInput;  //Add the user input into the end of the new array

If you're going to be adding a lot of elements, instead of making the array one element larger each time, you should consider doubling the size of the array whenever it gets full. This will save you copying all of the values over each time.

如果你要添加很多元素,而不是每次都使数组大一个元素,那么你应该考虑在数组变满时将其大小加倍。这将节省您每次复制所有值。

Here is another example of using copyOf()

这是使用copyOf()的另一个例子

#4


0  

List<Object> name = ArrayList<Object>();
name.add(userInput);

It's much better, more efficient. Also has convenient methods for use (especially add(Object), indexOf(Object), get(Object), remove(Object)).

它更好,更有效率。还有方便的使用方法(特别是add(Object),indexOf(Object),get(Object),remove(Object))。

#1


1  

You can't add one more element if your array is filled.

如果数组已填满,则无法再添加一个元素。

You need to build a bigger array and copy the values from the old array to the new one. That's where Arrays.copyOf comes in handy.

您需要构建一个更大的数组并将值从旧数组复制到新数组。这就是Arrays.copyOf派上用场的地方。

For performance reason, it would be better to add more than 1 empty cell each time you rebuild a new array. But basically, you will build your own implementation of ArrayList.

出于性能原因,每次重建新阵列时最好添加多个空单元格。但基本上,您将构建自己的ArrayList实现。

#2


0  

In an ArrayList you can just add another value without having to do anything. Internally, the ArrayList will create a new, larger array, copy the old one into it, and add the value to it.

在ArrayList中,您只需添加另一个值,而无需执行任何操作。在内部,ArrayList将创建一个新的更大的数组,将旧数组复制到其中,并将值添加到其中。

If you want to do this with an array, you will need to do this work yourself. As you are thinking, Arrays.copyOf() is a simple way to do that. For instance:

如果你想用数组做这个,你需要自己做这项工作。正如您所想,Arrays.copyOf()是一种简单的方法。例如:

    int[] a = {1,2,3,4,5};
    System.out.println(a.length); // this will be 5
    System.out.println(Arrays.toString(a)); // this will be [1, 2, 3, 4, 5]

    int[] b = Arrays.copyOf(a, 10);
    System.out.println(b.length); // this will be 10, half empty
    System.out.println(Arrays.toString(b)); // this will be [1, 2, 3, 4, 5, 0, 0, 0, 0, 0]

#3


0  

import java.util.Arrays;

int[] myArray = new int[]{1,2,3,4,5}; //The array of five

int[] myLongerArray = Arrays.copyOf(myArray, myArray.length + 1); //copy the original array into a larger one

myLongerArray[myLongerArray.length-1] = userInput;  //Add the user input into the end of the new array

If you're going to be adding a lot of elements, instead of making the array one element larger each time, you should consider doubling the size of the array whenever it gets full. This will save you copying all of the values over each time.

如果你要添加很多元素,而不是每次都使数组大一个元素,那么你应该考虑在数组变满时将其大小加倍。这将节省您每次复制所有值。

Here is another example of using copyOf()

这是使用copyOf()的另一个例子

#4


0  

List<Object> name = ArrayList<Object>();
name.add(userInput);

It's much better, more efficient. Also has convenient methods for use (especially add(Object), indexOf(Object), get(Object), remove(Object)).

它更好,更有效率。还有方便的使用方法(特别是add(Object),indexOf(Object),get(Object),remove(Object))。