How to enter arrays by constructor? Arrays java poo

1

I have to do a program to enter arrays in the main and then show the number plus its position, I wanted to enter arrays by the constructor and it does not leave me. Is there another way to do it?

public class IngresoDeEnteros {

private int numeros[];
private int dimension = 10;


public IngresoDeEnteros(int numeros []) {
    this.numeros = numeros;
    this.numeros = new int[dimension];
}

public void mostrarArrays() {

    int posicion = 0;
    for (int i = 0; i < numeros.length; i++) {

        posicion = i;


        System.out.println("vector : " +numeros[i] + "indice : "+ posicion);
    }


}

}

public class PruebaIngresoDeEnteros {

public static void main(String[] args) {
    IngresoDeEnteros array1[] = new IngresoDeEnteros{5, 10, 12,33 ,26 ,15,86,47,78,109};

    array1.mostrarArrays();


}

}

    
asked by computer96 05.12.2018 в 01:42
source

3 answers

4

There are several logic and syntax errors in your code:

  • dimension does not paint anything in the class if you plan to pass to the constructor an array already full with any dimension. The only thing I would do is to cover your class, limiting it to accept arrays of that dimension and of no other
  • In the constructor you cancel the future array of numbers that you would pass to it, when you do this: this.numeros = new int[dimension];
  • In the test you are using the constructor incorrectly: new IngresoDeEnteros{5, 10, 12,33 ,26 ,15,86,47,78,109}; . You must build the array of numbers well and once built, pass it to the constructor, using also the parentheses.
  • In mostrarArrays() , the variable position is redundant. You use it for something that i already does.

Correcting all that, we will have then:

Test

public class PruebaIngresoDeEnteros {
    public static void main(String[] args) {
        int[] intArray = new int[] {5, 10, 12,33 ,26 ,15,86,47,78,109};
        IngresoDeEnteros array1 = new IngresoDeEnteros(intArray);
    }
}

Class

public class IngresoDeEnteros {

    private int numeros[];

    public IngresoDeEnteros(int numeros []) {
        this.numeros = numeros;
    }

    public void mostrarArrays() {
        for (int i = 0; i < numeros.length; i++) {
            System.out.println("vector : " +numeros[i] + " indice : "+ i);
        }
    }

}

Result:

vector : 5 indice : 0
vector : 10 indice : 1
vector : 12 indice : 2
vector : 33 indice : 3
vector : 26 indice : 4
vector : 15 indice : 5
vector : 86 indice : 6
vector : 47 indice : 7
vector : 78 indice : 8
vector : 109 indice : 9
    
answered by 05.12.2018 / 02:01
source
1

If I understood your problem well this would be solved in the following way:

public class IngresoDeEnteros
{
    private int numeros[];

    public IngresoDeEnteros(int numeros [])
    {
        this.numeros = new int[numeros.length];
        System.arraycopy(numeros, 0, this.numeros, 0, numeros.length);
    }
    public void mostrarArrays()
    {
        for(int i = 0; i < numeros.length; i++)
        {
            System.out.println("vector: " +numeros[i] + " indice : " + i);
        }
    }
}

Then ...

public class PruebaIngresoDeEnteros
{
    public static void main(String[] args)
    {
        int ints[] = {5, 10, 12,33 ,26 ,15,86,47,78,109};
        IngresoDeEnteros array = new IngresoDeEnteros(ints);

        array.mostrarArrays();
    }
}
    
answered by 05.12.2018 в 02:01
1

public class TestIngresoDeEnteros {

public static void main(String[] args) {
    int[] array1={5, 10, 12,33 ,26 ,15,86,47,78,109};
    IngresoDeEnteros met = new IngresoDeEnteros(array1);
    met.mostrarArrays();
}

}

public class IngresoDeEnteros {

private int numeros[];
private final int dimension = 10;

public IngresoDeEnteros(int numeros[]) {
    this.numeros = new int[dimension];
    for (int i = 0; i < numeros.length; i++) {
        this.numeros[i] = numeros[i];
    }
}

public void mostrarArrays() {

    int posicion = 0;
    for (int i = 0; i < numeros.length; i++) {

        posicion = i;

        System.out.println("vector : " + numeros[i] + " indice : " + posicion);
    }

}

}

you had to create your arrangement first and then send it, you can not send values even worse with brackets, you usually use parentheses: D

    
answered by 05.12.2018 в 02:06