1

Today, I have a question about the list. I want to put the variables in the array into the another array. For example, if there is a int[] list1 and int[] list2 = {1,2,3,4} and int[] list3 = {5,6,7}, I want to put the variables in list2 and list3 into the list1, making the list1 into {1,2 + 5, 3 + 6, 4 + 7}. Below is the code that I have made:

public int function(int[] parameter) {
    int[] intlist;
    for (int i = 0; i < intlist.length; i++) {
        intlist = addVariable(int[] anotherlist); //the function addVariable(int[] parameter) gets a
        //int[] as a paremeter, makes a new int[](which has the same size as the parameter) at the 
        //inside of the function, add parameter's each and every varibles into the new int[] and 
        //returns the new int[]. and anotherlist keeps changing in the for statement. This is the 
        //function that I want to make.
    }
    return intlist;
}

Next is the code of addVariable:

public static int[] addVariables(int[] intlist) {
        int[] intlist2 = new int[intlist.length];
        for(int i = 0; i < intlist.length; i++) {
            intlist2[i] += intlist[i];
        }
        return intlist2;
}

So, what I want to do is making the intlist whole, by using for statement and addVariable function. But, the addVariable fuction is not complete, as the two lists can have different size, and the function that I've made didn't consider this. Also, the changes that I've made in the for statement doesn't last, so this is a problem as well. How can I fix this situation? Please help!

6
  • Should the addition begin from the last element of longer array? And, is it assured that list2 is longer than list3? Commented Mar 31, 2020 at 9:10
  • @MyBug18 um, so what I mean is that there is a list1, and list2, list3, list4, and list2,3,4's size is 3,4,5, the list1's size must be the maximum size between the list2,3,4. So, it must look like this: list1[0] = list2[0] + list3[0] + list4[0], ..... list1[3] = list2[3] + list3[3], .... Commented Mar 31, 2020 at 9:27
  • The line "intlist = addVariable(int[] anotherlist);" doesn't make sense. You need to pass addVariable an existing list. Commented Mar 31, 2020 at 10:17
  • @NomadMaker Yeah, I've thought so too, after looking at it for some time... I've fixed it know! Commented Mar 31, 2020 at 10:26
  • If one of the answers helped you, please accept it. If you came up with your own solution that wasn't an answer, then please answer it yourself and eventually accept that answer. Commented Mar 31, 2020 at 10:28

1 Answer 1

0

This is the code you need

public class Test {

public static void main(String args[]) {
    int[] intlist = {1, 2, 3, 4};
    int[] anotherlist = {5, 6, 7};


    Test.addVariables(intlist, anotherlist);
    for (int i : intlist) {
        System.out.println(i);
    }

}

public static void addVariables(int[] intlist, int[] anotherlist) {

    for (int i = intlist.length - 1, j = anotherlist.length - 1; i >= 0 && j >= 0; i--, j--) {
        intlist[i] += anotherlist[j];
    }

  }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.