有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么是ArraysUtils。addElement没有正常工作吗?

我有一段代码,它要求输入数字,然后将它们引入一个数组,然后,我必须分离正、负和空

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

例如,我开始编写代码,引入5个数字(1,2,3,4,5),然后使用将正数分配给正数数组、将负数分配给负数数组和将空数分配给空数组的方法

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

enter image description here

我每次给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}

还是我错了

我用的是ApacheComons

谢谢


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

共 (1) 个答案

  1. # 1 楼答案

    当你使用新的int[4]时,你会得到一个类似[0,0,0,0]的数组,因为int是基本的,所以它不能为null

    阿拉尤蒂尔。add不会替换这些0值,因为它们是有效的条目,所以它只是将新的数字附加到数组中

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