部分填充的数组 - 获得最小值和toString

时间:2014-01-25 22:10:28

标签: java arrays loops java.util.scanner minimum

我正在为我的一项任务获得奖金。我应该创建一个非常大的数组,但用户不必使用数组中的所有插槽。以前我使用了一个增强的for循环来填充所有索引的常规数组。我不确定如何改变它以适应新的标准。

每当我在测试仪中输入数字时,toString返回空点,最小值返回0,因为空点的值为0。

public class NumberSetBonus
{   
    private int[] numbers;
    private int count;

    /**
     * Constructs an empty NumberSet of a specified size
     * @param size the number of elements in the set
     */
    public NumberSetBonus(int size)
    {
        numbers = new int[size];
        count = 0;
    }

    /**
     * addNumber adds a number to the number set 
     * @param num new number
     */
    public void addNumber(int num)
    {
        numbers[count] = num;
        count++; 
    }

    /**
     * getMin finds the minimum value stored in the NumberSet
     * @return the minimum value
     * precondition:  array is full of values
     */
    public int getMin()
    {
        int min = numbers[0];

        for(int n : numbers)
            if(n < min)
                min = n;

        return min;
    }

    /**
     * getMax finds the maximum value stored in the NumberSet
     * @return the maximum value
     * precondition:  array is full of values
     */
    public int getMax()
    {
        int max = numbers[0];

        for(int n : numbers)
            if(n > max)
                max = n;

        return max;
    }

    /** 
     * find determines whether a specified value exists in the set.
     * @param num the number to find
     * @return whether the value exists
     */
    public boolean find(int num)
    {
        boolean find = true;

        for(int n : numbers)
            if(n == num)
                find = true;    

        return find;    
    }

    /**
     * getSum calculates the sum of the values in the set.
     * @return the sum
     */
    public int getSum()
    {
        int sum = 0;

        for(int n : numbers)
            sum += n;

        return sum;
    }

    /**
     * getMean calculates the mean of the values in the set.
     * @return the mean as a double
     * precondition:  array is full of values
     * NOTE: use the length field of the array
     */ 
    public double getMean()
    {
        return getSum() / (double) numbers.length;
    }

    /**
     * count counts the occurrence of a specified number within the set.
     * @param num the number to find
     * @return the number of times num occurs in the set
     */
    public int count(int num)
    {
        int quantity = 0;

        for(int n : numbers)
            if(n == num)
                quantity++;

        return quantity;
    }

    /**
     * toString returns a String containing the values in the set on a 
     * single line with a space between each value.
     * @return the String version of the set
     */
    public String toString()
    {
        String set = " ";

        for(int n : numbers)
            set += n + " "; 
        return set;
    }
}

这是我的测试人员,我要求用户输入,toString()返回0的位置

public class NumberSetBonusTester
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        final int LENGTH = 100;
        NumberSetBonus list = new NumberSetBonus(LENGTH);
        int arraySize = 0;

        System.out.print("You can enter up to 100 numbers in this array. \nType in a negative number if you want to stop adding numbers.\nEnter a number: ");

        while(arraySize < LENGTH)
        {
            int number = input.nextInt();
            if(number <= 0)
                break;
            list.addNumber(number);
            arraySize++;
        }           

        System.out.print("\nset 1:\t\t");
        System.out.println(list);
        System.out.println("list sum:\t" + list.getSum());
        System.out.println("list mean:\t" + list.getSum() / arraySize);
        System.out.println("list max:\t" + list.getMax());
        System.out.println("list min:\t" + list.getMin());
        System.out.println();
        System.out.print("Enter a number to find ==> ");
        int searchNum = input.nextInt();
        if (list.find(searchNum))
            System.out.println(searchNum + " is in the set " + list.count(searchNum) + " times");
        else
            System.out.println(searchNum + " is not in the set");
    }
}

1 个答案:

答案 0 :(得分:2)

您可以简单地将所有for循环更改为for (int i = 0; i < count; i++)形式 - 这样循环只会循环实际设置的数字,而不是数组中的所有数字。

BTW:如果将来需要一些可变大小的数组,可以使用List(来自java.util包)。在您的示例中,我将使用ArrayList<Integer>。这是一个在内部使用数组的列表,但如果数组太小(通过创建新数组并复制内容),则会增加其大小。