[英]How can I add new integers to replace the old integers to my already-existing Array?
Here is the program task:这是程序任务:
Write a method called collapse that accepts an array of integers as a parameter and returns a new array containing the result of replacing each pair of integers with the sum of that pair.编写一个名为 collapse 的方法,它接受一个整数数组作为参数,并返回一个新数组,该数组包含用该对整数的总和替换每对整数的结果。
For example, if an array called list stores the values例如,如果名为 list 的数组存储值{7, 2, 8, 9, 4, 13, 7, 1, 9, 10}
then the call of collapse(list)
should return a new array containing: {9, 17, 17, 8, 19}
.那么对collapse(list)
的调用应该返回一个包含: {9, 17, 17, 8, 19}
的新数组。
The first pair from the original list is collapsed into 9 (7 + 2), the second pair is collapsed into 17 (8 + 9), and so on.原始列表中的第一对折叠为 9 (7 + 2),第二对折叠为 17 (8 + 9),依此类推。 If the list stores an odd number of elements, the final element is not collapsed.如果列表存储奇数个元素,则最后一个元素不会折叠。
For example, if the list had been {1, 2, 3, 4, 5}
, then the call would return {3, 7, 5}
.例如,如果列表是{1, 2, 3, 4, 5}
,则调用将返回{3, 7, 5}
。 Your method should not change the array that is passed as a parameter.您的方法不应更改作为参数传递的数组。
Here is my currently-written program:这是我目前编写的程序:
public static int[] collapse(int[] a1) {
int newArrayLength = a1.length / 2;
int[] collapsed = new int[newArrayLength];
int firstTwoSums = 0;
for (int i = 0; i < a1.length-1; i++) {
firstTwoSums = a1[i] + a1[i+1];
collapsed[collapsed.length-1] = firstTwoSums;
}
return collapsed;
}
I pass in an array of {7, 2, 8, 9, 4, 13, 7, 1, 9, 10}
and I want to replace this array with {9, 17, 17, 8, 19}
.我传入了一个{7, 2, 8, 9, 4, 13, 7, 1, 9, 10}
数组,我想用{9, 17, 17, 8, 19}
替换这个数组。
Note: {9, 17, 17, 8, 19}
will be obtained through the for-loop that I have written.注意: {9, 17, 17, 8, 19}
会通过我写的for循环得到。
Currently, I am having trouble with adding the integers I obtained to my "collapsed" array.目前,我无法将获得的整数添加到“折叠”数组中。 It'd be a great help if you could help me or at least give me some guidance on how to do this.如果你能帮助我或者至少给我一些关于如何做到这一点的指导,那将是一个很大的帮助。
Thanks in advance!提前致谢!
Using使用
collapsed[collapsed.length-1] = firstTwoSums;
The sum of your numbers will be always be put in the same index of the collapsed array, because collapsed.length - 1 is a constant value.您的数字总和将始终放在折叠数组的相同索引中,因为 collapsed.length - 1 是一个常数值。
Try creating a new variable starting at zero, that can be incremented each time you add a sum to collapsed.尝试创建一个从零开始的新变量,每次将总和添加到折叠时都可以递增。 For instance,例如,
int j = 0;
for(...) {
...
collapsed[j++] = firstTwoSums;
}
First you have to understand what is going on.首先,您必须了解正在发生的事情。
You have an array of certain size
where size can either be even
or odd
.您有一个特定size
的数组,其中大小可以是even
或odd
。 This is important because you are using a1.length/2
to set the size for new array
, so you will also have to check for odd and even values to set the size right else it won't work for odd sized arrays.这很重要,因为您使用a1.length/2
来设置new array
的大小,因此您还必须检查奇数和偶数值以正确设置大小,否则它不适用于奇数大小的数组。 Try a few cases for better understanding.尝试几个案例以更好地理解。
Here's a way of doing it.这是一种方法。
public static int[] collapseThis(int[] array) {
int size = 0;
if(isEven(array.length))
size = array.length/2;
else
size = array.length/2+1;
int[] collapsedArray = new int[size];
for(int i=0, j=0; j<=size-1; i++, j++) {
if(j==size-1 && !isEven(array.length)) {
collapsedArray[j] = array[2*i];
}
else {
collapsedArray[j] = array[2*i]+array[2*i+1];
}
}
return collapsedArray;
}
private static boolean isEven(int num) {
return (num % 2 == 0);
}
I think this is a convenient answer.我认为这是一个方便的答案。
public static void main(String[] args){
int[] numbers = {1,2,3,4,5};
int[] newList = collapse(numbers);
System.out.println(Arrays.toString(newList));
}
public static int[] collapse(int[] data){
int[] newList = new int[(data.length + 1)/2];
int count = 0;
for (int i = 0; i < (data.length / 2); i++){
newList[i] = data[count] + data[count + 1];
System.out.println(newList[i]);
count = count + 2;
}
if (data.length % 2 == 1){
newList[(data.length / 2)] = data[data.length - 1];
}
return newList;
}
i would combine the cases for the array with either odd or even elements together as below:我会将数组的情况与奇数或偶数元素组合在一起,如下所示:
public static int[] collapse(int[] a1) {
int[] res = new int[a1.length/2 + a1.length % 2];
for (int i = 0; i < a1.length; i++)
res[i/2] += a1[i];
return res;
} }
public static int[] collapse(int[] a1) {
int newArrayLength = a1.length / 2;
int[] collapsed;
if(a1.length%2 == 0)
{
collapsed = new int[newArrayLength];
}
else
{
collapsed = new int[newArrayLength+1];
collapsed[newArrayLength] = a1[a1.length-1];
}
int firstTwoSums = 0;
for (int i = 0; i < newArrayLength; i++) {
firstTwoSums = a1[i*2] + a1[i*2+1];
collapsed[i] = firstTwoSums;
}
return collapsed;
}
I modified your code and you may try it first.我修改了你的代码,你可以先试试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.