为什么 ArraysUtils.addElement 没有正常工作?

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

我有这段代码要求输入数字,然后将它们引入数组,然后,我必须将正数、负数和空值分开。

我在创建每种类型的数组时遇到问题。

例如,我开始代码,我引入了 5 个数字 (1,2,3,4,5),然后转到将正数分配给 positiveArray,将负数分配给 negativeArray,将空值分配给nulledArray.

但是当我打印时,例如,positiveArray,只在最后的索引处添加

我读到每次调用 ArrayUtils.add() 时,它应该将数字放在末尾,但不像我得到的那样,应该是这样的:

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

数字 = {1,2,3,4}

还是我错了?

我正在使用 ApacheCommons

谢谢


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

问题是您在每次迭代时都重新创建了一个新数组,例如这一行(在 cantidadPositivos() 中):

numerosPositivos = new int[pos];

您应该在进入 for 循环之前定义这些数组。

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

当您使用 new int[4] 时,您会得到一个类似于 [0, 0, 0, 0] 的数组,因为 int 是原始的,所以它不能为 null。

ArrayUtil.add 不会替换那些 0 值,因为它们是有效条目,所以它只是将您的新数字附加到数组中。

所以我建议从一个空数组开始。