Search code examples
javaarraysapache-commons

Why is ArraysUtils.addElement not working as it should be?


I have this code that asks for numbers and then introduces them into an array, then, I have to separate positives, negatives and nulls.

I'm having trouble with creating the array of each type.

For example, I start the code, I introduce 5 numbers(1,2,3,4,5), then goes to the method that assings the positives to the positiveArray, negatives to the negativeArray and nulls to the nulledArray.

But when I print, for example, positiveArray, only adds at the final index

enter image description here

I read that everytime I call ArrayUtils.add(), it should be placing the number at the end, but not like what I'm getting, that should be like this:

int[] numbers= new int[4];
numbers = ArrayUtils.add(numbers , 1);
numbers = ArrayUtils.add(numbers , 2);
numbers = ArrayUtils.add(numbers , 3);
numbers = ArrayUtils.add(numbers , 4);

numbers = {1,2,3,4}

Or am I wrong?

I'm using ApacheCommons

Thanks


import java.util.Arrays;

import javax.swing.JOptionPane;

import org.apache.commons.lang3.ArrayUtils;

public class das {

    private int cantidadElementos = 0;
    private int pos = 0, neg = 0, nulos = 0; //contadores de los números positivos y negativos
    private int[] numeros = new int[10]; //array que contendrá los números leídos por teclado
    private int[] numerosNegativos;
    private int[] numerosPositivos;
    private int[] numerosNulos;

    public static void main(String[] args) {
        das das = new das();
        das.agregarElementos();
        das.cantidadPositivos();

    }
public void agregarElementos(){
    int i = 0;
    for (i = 0; i < 10; i++) {
        try {
            numeros[i] = (Integer.parseInt(JOptionPane.showInputDialog(null, "Introduce un valor", "Agregando elemento ["+cantidadElementos+"]", JOptionPane.QUESTION_MESSAGE)));
            if(numeros[i] > 0)
            {
                pos++;          
            }
            else if (numeros[i] < 0)
            {
                neg++;
            }
            else
            {
                nulos++;
            }

            cantidadElementos++;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Has agregado "+cantidadElementos+" elementos.", "Información que cura", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
    }

}


public void cantidadPositivos(){
    int i = 0;
    for(i = 0; i < cantidadElementos; i++)
    {
        if(numeros[i] >= 1)
        {
            numerosPositivos = new int[pos];
            numerosPositivos = ArrayUtils.add(numerosPositivos, numeros[i]);
            System.out.println(Arrays.toString(numerosPositivos));

        }
        else if (numeros[i] <=-1)
        {
            numerosNegativos = new int[neg];
            numerosNegativos = ArrayUtils.add(numerosNegativos, numeros[i]);
        }
        else
        {
            numerosNulos = new int[nulos];
            numerosNulos = ArrayUtils.add(numerosNulos, numeros[i]);
        }

    }
    JOptionPane.showMessageDialog(null, "De los [ "+cantidadElementos +" ] elementos en el vector:\n\nPositivos: "+ pos +"\n\nNegativos: "+ neg +"\n\nNulos: " + nulos + "\n", "Información que cura", JOptionPane.INFORMATION_MESSAGE, null);
}
}

Solution

  • The problem is that you recreate a new array at each iteration, like this line (in cantidadPositivos()):

    numerosPositivos = new int[pos];
    

    You should define those arrays before entering the for cycle.

    public void cantidadPositivos(){
        int i = 0;
        numerosPositivos = new int[pos];
        numerosNegativos = new int[neg];
        numerosNulos = new int[nulos];
        for(i = 0; i < cantidadElementos; i++)
        {
            if(numeros[i] >= 1)
            {
                numerosPositivos = ArrayUtils.add(numerosPositivos, numeros[i]);
                System.out.println(Arrays.toString(numerosPositivos));   
            }
            else if (numeros[i] <=-1)
            {
                numerosNegativos = ArrayUtils.add(numerosNegativos, numeros[i]);
            }
            else
            {
                numerosNulos = ArrayUtils.add(numerosNulos, numeros[i]);
            }
    
        }
        JOptionPane.showMessageDialog(null, "De los [ "+cantidadElementos +" ] elementos en el vector:\n\nPositivos: "+ pos +"\n\nNegativos: "+ neg +"\n\nNulos: " + nulos + "\n", "Información que cura", JOptionPane.INFORMATION_MESSAGE, null);
    }