简体   繁体   English

如何添加新整数来替换旧整数到我已经存在的数组中?

[英]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的数组,其中大小可以是evenodd 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.

相关问题 如何在Java中将具有整数的字符串转换为具有整数的数组? - How can I convert a string with integers to array with integers in java? 当索引已经初始化时,如何在二维数组中添加整数? - How to add integers in a 2D array when the indexes are already initialized? 如何在此示例中已存在的新jpanel上添加? - How can I add new jpanel on already existing in this example? 如何将整数添加到数组中的整数 - How to add integer to integers in array 如何在Android上用新的旧适配器替换旧的列表适配器? 它不断将新的附加到旧的 - How can I replace my old list adapter with a new one on Android? It keeps appending new to old one 如何在数组中交换两个整数,而我的方法从main中获取两个整数和一个数组? - How do I swap two integers in an array, where my method takes in two integers and an array from main? 在一个时间间隔后如何生成新的随机整数? - How can I generate new random integers after a time interval? 如何将Object数组转换为整数数组数组 - How can I cast an Object array to an array of arrays of integers 如何使用 switch 语句在 Java 中添加两个整数 - How can I use switch statements to add two integers in Java 在这种情况下,如何从字符串中获取整数? - How can I get integers from strings in my situation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM