首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何删除数组中的0?

如何删除数组中的0?
EN

Stack Overflow用户
提问于 2022-11-20 16:22:12
回答 2查看 38关注 0票数 1

我试图在数组中显示值,但是其中有0。使用for循环来计算温度的数量和温度的值。

这是我使用的代码:

代码语言:javascript
复制
import java.util.*;

public class Array1dTemperature {
    static Scanner in = new Scanner (System.in);
    static Random rng = new Random ();
    public static void main(String[] args) 
    {
        System.out.println("This program does a temperature check.");
        System.out.println("Input the desired number of temperatures...");
        int size = in.nextInt();
        int[] temp = new int [size];
            for (int i=0; i<temp.length; i++)
                temp[i] = 1+rng.nextInt(100);
        System.out.println("The data file goes as: " + Arrays.toString(temp));
        Checker(temp);
    }

    static void Checker (int temp[])
    {
        int hot[] = new int [10] ; int []pleasant = new int [10]; int []cold  = new int [10];
        int H = 0; int P = 0; int C = 0;
        for (int i=0; i<temp.length;i++) {
            if (temp[i]>=85) { 
                hot[i] = temp[i];
                H++;
            }
            else if (temp[i]>=60&&temp[i]<84) {
                pleasant[i] = temp[i];
                P++;
            }
            else if (temp[i]<60) {
                cold[i] = temp[i];
                C++;
            }
        }
        System.out.println("number of hot: "+ H + ", Recorded temps are: " + Arrays.toString(hot) );
        System.out.println("number of cold: "+ C + ", Recorded temps are: " + Arrays.toString(cold));
        System.out.println("number of pleasant: "+ P + ", Recorded temps are: " + Arrays.toString(pleasant));
    }
}

我试图更改各个数组本身的值,但每当我试图打印输出时,它就会超出界限。我本可以使用"Arraylist“来更新数组,但是这个特定的练习问题禁止使用这样的数组。

这是输出

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-11-20 16:35:39

所以数组对于固定长度的数据是很好的,在那里你可以预先知道会有多少数据。在您的示例中,您不知道在您的热/冷/愉快数组中会有多少“真实”数据。在现实世界中,您将使用另一种数据结构(如ArrayList),而不是用您所拥有的数据填充它们。

如果您一定要使用数组,那么您需要先在输入数组上循环一次,以了解每个数组有多少个,初始化适当大小的hot/etc数组,然后再循环分配它们。但是,您不能只将输入数组中的索引分配给相同的索引--而是要跟踪hot (或其他什么)数组中的下一个开放空间,每次写入该计数器,然后递增该计数器。

票数 0
EN

Stack Overflow用户

发布于 2022-11-20 16:42:17

您需要使用HPC作为hotpleasantcold数组的单独索引,同时填充数组,并可能应用Arrays.copyOf消除这些数组尾部的零。

此外,也有边缘的情况是固定的设置温度类型。

代码语言:javascript
复制
static void Checker (int temp[]) {
    int[] hot = new int[temp.length]; 
    int[] pleasant = new int[temp.length]; 
    int[] cold = new int[temp.length];

    int H = 0; int P = 0; int C = 0;
    for (int i = 0; i < temp.length; i++) {
        if (temp[i] >= 85) {
            hot[H++] = temp[i]; 
        }
        else if (temp[i] >= 60) {
            pleasant[P++] = temp[i];
        }
        else {
            cold[C++] = temp[i];
        }
    }
    if (H < hot.length) hot = Arrays.copyOf(hot, H);
    if (P < pleasant.length) pleasant = Arrays.copyOf(pleasant, P);
    if (C < cold.length) cold = Arrays.copyOf(cold, C);

    System.out.println("number of hot: "+ H + ", Recorded temps are: " + Arrays.toString(hot) );
    System.out.println("number of cold: "+ C + ", Recorded temps are: " + Arrays.toString(cold));
    System.out.println("number of pleasant: "+ P + ", Recorded temps are: " + Arrays.toString(pleasant));
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74509850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档