【问题标题】:How do I go about displaying only the logical sizes of my 3 arrays?如何只显示我的 3 个数组的逻辑大小?
【发布时间】:2021-12-17 04:09:00
【问题描述】:

我有 3 个数组,oddList、negativeList 和 evenList,它们从 masterArray 中获取数字并相应地对它们进行排序。当我去打印 3 个数组时,如果数组中的某些索引未填充,它会自动填充 0.0 我需要弄清楚如何摆脱这些零并仅使用数组的逻辑大小。

public static void negoddeven(double masterArray[]){
    int z;
    int size = 0;
    int sum = 0;
    double[] negativeList = new double[10], oddList = new double[10], evenList = new double[10];

    for(z = 0; z < masterArray.length; z++){
        for(int i = 0; i < size; i++){
        if(masterArray[z]%2 != 0){
            sum+= oddList[z];
            if(size < oddList.length){
            oddList[z] = masterArray[z];
            size++;
        }}
        else if(masterArray[z] < 0){
            if(size < negativeList.length){
            negativeList[size] = masterArray[z];
            size++;
        }}
        else if(masterArray[z]%2 == 0){
            if(size < negativeList.length){
            evenList[size] = masterArray[z];
            size++;
        }}
        }
    }
    System.out.print("\nThe numbers in the odd list are: " + Arrays.toString(oddList));
    System.out.print("\nThe numbers in the negative list are: " + Arrays.toString(negativeList));
    System.out.print("\nThe numbers in the even list are: " + Arrays.toString(evenList) + "\n");
    }

【问题讨论】:

  • "它被自动替换为 0.0" -- 没有被“替换”。数组中的位置不能为空,因此在首次创建数组时填充 0.0。如果您想要一个动态大小的容器,请考虑改用ArrayList

标签: java arrays for-loop


【解决方案1】:

使用动态List而不是固定大小的数组来收集masterArray中的所有数据会更方便。

此外,负值应与奇偶校验分开计算。

public static void negOddEven(double ... masterArray) {
    int sum = 0;
    List<Double> 
        negativeList = new ArrayList<>(), 
        oddList = new ArrayList<>(), 
        evenList = new ArrayList<>();

    for (int i = 0; i < masterArray.length; i++) {
        double z = masterArray[i];
        
        if (z < 0) {
            negativeList.add(z);
        }
        if (z % 2 == 0) {
            evenList.add(z);
        } else {
            oddList.add(z);
            sum += z;
        }
    }
    System.out.println("The numbers in the odd list are: " + oddList + "; sum = " + sum);
    System.out.println("The numbers in the negative list are: " + negativeList);
    System.out.println("The numbers in the even list are: " + evenList + "\n");
}

测试:

negOddEven(1, 2, 3, 0, 4.5, -2, -0.0, -12.2);

输出:

The numbers in the odd list are: [1.0, 3.0, 4.5, -12.2]; sum = -4
The numbers in the negative list are: [-2.0, -12.2]
The numbers in the even list are: [2.0, 0.0, -2.0, -0.0]

如果由于某些限制而不允许使用列表并且只应使用数组,则需要为所有数组的实际长度引入单独的计数器。使用Arrays.copyOf 切断数组中未使用的单元格也是有意义的:

public static void negOddEven(double ... masterArray) {
    int sum = 0;
    double[] 
        negList  = new double[10], 
        oddList  = new double[10], 
        evenList = new double[10];
    int negSz = 0, oddSz = 0, evenSz = 0;

    for(int i = 0; i < masterArray.length; i++) {
        double z = masterArray[i];
        
        if (z < 0) {
            if (negSz < negList.length) {
                negList[negSz++] = z;
            }
        }
        if (z % 2 == 0) {
            if (evenSz < evenList.length) {
                evenList[evenSz++] = z;
            }
        } else if (oddSz < oddList.length) {
            oddList[oddSz++] = z;
            sum += z;
        }
    }
    if (negSz  < 10) negList  = Arrays.copyOf(negList,  negSz);
    if (oddSz  < 10) oddList  = Arrays.copyOf(oddList,  oddSz);
    if (evenSz < 10) evenList = Arrays.copyOf(evenList, evenSz);

    System.out.println("The numbers in the odd list are: " + Arrays.toString(oddList) + "; sum = " + sum);
    System.out.println("The numbers in the negative list are: " + Arrays.toString(negList));
    System.out.println("The numbers in the even list are: " + Arrays.toString(evenList) + "\n");
}

【讨论】:

    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    相关资源
    最近更新 更多