java 如何将参数添加到数组的末尾?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5574204/
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 do I add a parameter to the end of an array?
提问by user695696
I am working on this NumberList class which represents a list of integers. The NumberList object has just one instance variable, which is a reference to an array of int values. One of the methods I need to implement is suppose to add the parameter to the end of the list by:
我正在研究这个 NumberList 类,它表示一个整数列表。NumberList 对象只有一个实例变量,它是对 int 值数组的引用。我需要实现的方法之一是假设通过以下方式将参数添加到列表的末尾:
a) creating another array that is one unit larger than the existing one
a) 创建另一个比现有数组大一个单位的数组
b) Copying all of the elements from the existing array over to the new one
b) 将现有数组中的所有元素复制到新数组中
c) Adding the parameter to the end of the new array
c) 将参数添加到新数组的末尾
d) Re-assigning the instance variable "values" so that it refers to the new array.
d) 重新分配实例变量“values”,使其指向新数组。
Here is my attempt at it. There are no errors but I feel like it is incorrect especially the part where I try to add number to the end of anotherArray. The parameter I am referring to is "number", an int
这是我的尝试。没有错误,但我觉得这是不正确的,尤其是我尝试在 anotherArray 末尾添加数字的部分。我指的参数是“数字”,一个整数
public void add(int number) {
int[] anotherArray;
int newLength = values.length + 1;
anotherArray = new int[newLength];
for (int i = 0; i <values.length; i++)
values[i] = anotherArray[i];
for (int i = 0; i < anotherArray[i]; i++)
anotherArray[i] += number;
values = new int[anotherArray.length];
}
回答by Greg Hewgill
First, you've got the assignment backwards. It should be this way around:
首先,你把任务倒过来了。应该是这样的:
for (int i = 0; i < values.length; i++)
anotherArray[i] = values[i];
This assigns fromvalues
toanotherArray
. Second, you want to setthe new value in anotherArray
, like this:
这分配fromvalues
到anotherArray
。其次,您想在 中设置新值anotherArray
,如下所示:
anotherArray[newLength - 1] = number;
Finally,
最后,
values = anotherArray;
Here's another way to write that code using System.arraycopy
:
这是使用System.arraycopy
以下代码编写该代码的另一种方法:
public void add(int number) {
int[] anotherArray = new int[values.length + 1];
System.arraycopy(values, 0, anotherArray, 0, values.length);
anotherArray[values.length] = number;
values = anotherArray;
}
回答by nevets1219
You don't need two for
loops to do this because you just need to copy the elements from the old array into the new array once.
您不需要两个for
循环来执行此操作,因为您只需要将旧数组中的元素复制到新数组中一次。
You also do not want to do values = new int[anotherArray.length];
because that is essentially declaring a new array with no values set.
您也不想这样做,values = new int[anotherArray.length];
因为这实际上是在声明一个没有设置值的新数组。
The logic should look something like:
逻辑应该类似于:
newArray = new Array[old_length + 1];
for ( i from 0 to old_length ) {
newArray[i] = values[i];
}
newArray[old_length] = new_element
values = newArray
回答by Dastyruck
It looks like for the most part, you are on the right track, but it starts to derail when you try to add to the end of the new array...
在大多数情况下,您看起来是在正确的轨道上,但是当您尝试添加到新数组的末尾时,它开始出轨......
From what I can tell, all you need is this...
据我所知,你只需要这个......
`public void add(int number) { int[] anotherArray; int newLength = values.length + 1;
`public void add(int number) { int[] anotherArray; int newLength = values.length + 1;
anotherArray = new int[newLength];
for (int i = 0; i <values.length; i++)
anotherArray[i] = values[i];
anotherArray[newLength-1] = number;
values = anotherArray.length;
} `
}`
回答by mikera
I'd suggest a few changes to the implementation of your NumberList class:
我建议对 NumberList 类的实现进行一些更改:
- Add a "count" instance variable to your NumberList class, which represents the number of integers in the list. This will be useful in the case that you want to allocate a longer array than you currently have integers, e.g. you can have a length 10 array initially but have count==0
- Then if you want to add a new integer and still have space in the array, all you need to do is put the integer in the right place in the array and do count++
- When you need to reallocate the array (i.e. if count == array.length when you try to add a new integer), then you should make the new array quite a bit larger, e.g. double the size of the previous array. This will avoid reallocating the array with every single integer added.
- Use System.arraycopy to copy the old array values into the new array (with a length equal to "count")
- 将“count”实例变量添加到您的 NumberList 类,它表示列表中的整数数量。如果您想分配比当前整数更长的数组,这将很有用,例如,您最初可以拥有一个长度为 10 的数组,但 count==0
- 然后如果你想添加一个新的整数并且数组中还有空间,你需要做的就是将整数放在数组中的正确位置并执行 count++
- 当您需要重新分配数组时(即如果当您尝试添加一个新整数时 count == array.length),那么您应该使新数组大一些,例如将前一个数组的大小加倍。这将避免在添加每个整数后重新分配数组。
- 使用 System.arraycopy 将旧数组值复制到新数组中(长度等于“count”)
回答by MikeU
Look at parts b, c, and d in your algorithm, and compare them carefully with your code.
查看算法中的 b、c 和 d 部分,并将它们与您的代码仔细比较。
The first for-loop performs part b. But compare your code with the statement "Copying all of the elements from the existing array over to the new one." Is that what your code is doing? Not quite, but almost. Your code assigns values from the new array to the existing array.
第一个 for 循环执行 b 部分。但是将您的代码与语句“将现有数组中的所有元素复制到新数组中”进行比较。那是你的代码在做什么吗?不完全,但几乎。您的代码将值从新数组分配给现有数组。
Now think about the statement in part c. Does this require a loop? No, it does not. But first, let's discuss what's wrong with this loop, as a learning exercise. First, it may generate an IndexOutOfBoundsException because your loop condition is i < anotherArray[i]
. The values in the array have nothing to do with their size, so if all the values are large, i
will increment past the end of the array. Also, why are you adding number
to each element?
现在考虑 c 部分的陈述。这需要循环吗?不,不是的。但首先,作为一个学习练习,让我们讨论这个循环有什么问题。首先,它可能会生成一个 IndexOutOfBoundsException,因为您的循环条件是i < anotherArray[i]
。数组中的值与它们的大小无关,因此如果所有值都很大,i
则将增加超过数组的末尾。另外,为什么要添加number
到每个元素?
Now, as for fixing it, take a look at statement c. It says to add it to the end of the new array. In this context (and in all contexts involving data structures), "add" means "insert" and not "+". You created a new array with one extra element; that element is current empty. This new element is at the end, and needs to hold number
.
现在,至于修复它,看看语句 c。它说将它添加到新数组的末尾。在此上下文中(以及在所有涉及数据结构的上下文中),“添加”表示“插入”而不是“+”。您创建了一个带有一个额外元素的新数组;该元素当前为空。这个新元素在最后,需要保持number
。
Finally, your last statement just makes values
point to a new, empty array. Instead, you should make the values
reference point to the array that you already created and filled with values.
最后,您的最后一条语句只是values
指向一个新的空数组。相反,您应该将values
引用指向您已经创建并填充了值的数组。
回答by Adi Mor
you don't need the second for loop, you just need to add the last number at the last position of the array.
你不需要第二个 for 循环,你只需要在数组的最后一个位置添加最后一个数字。
public void add(int number) {
int[] anotherArray;
int newLength = values.length + 1;
anotherArray = new int[newLength];
for (int i = 0; i <values.length; i++)
values[i] = anotherArray[i];
//for (int i = 0; i < anotherArray[i]; i++)
anotherArray[anotherArray.lenth-1] = number;
values = new int[anotherArray.length];
}
}