Adición de una variables en la matriz en otra matriz

Sangcheol Yu:

Hoy en día, tengo una pregunta acerca de la lista. Quiero poner las variables en la matriz en la matriz otro. Por ejemplo, si hay una lista1 int [] y int [] list2 = {1,2,3,4} y int [] list3 = {5,6,7}, quiero poner las variables en list2 y list3 en el lista1, haciendo que la lista1 en {1,2 + 5, 3 + 6, 4 + 7}. A continuación se muestra el código que he hecho:

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;
}

El siguiente es el código de 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;
}

Por lo tanto, lo que quiero hacer es hacer la intlist conjunto, mediante el uso de la declaración y la función addVariable. Sin embargo, el fuction addVariable no es completa, ya que las dos listas pueden tener diferente tamaño, y la función que he hecho no tuvo en cuenta esto. Además, los cambios que he hecho en la sentencia for no dura, por lo que este es un problema también. ¿Cómo puedo solucionar esta situación? ¡Por favor ayuda!

Thanga:

Este es el código que necesita

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];
    }

  }

}

Supongo que te gusta

Origin http://10.200.1.11:23101/article/api/json?id=374928&siteId=1
Recomendado
Clasificación