如何从数组中的单个值或一系列值中查找元素编号

时间:2012-10-28 18:17:05

标签: java arrays

这是即将到来的大学实践的代码:

import java.util.Random;

public class Practical4_Assessed 
{

public static void main(String[] args) 
{

    Random numberGenerator = new Random ();
    int[] arrayOfGenerator = new int[100];
    int[] countOfArray     = new int[10];
    int count;

    for (int countOfGenerator = 0; countOfGenerator < 100; countOfGenerator++)
    {
        count = numberGenerator.nextInt(10);
        countOfArray[count]++;
        arrayOfGenerator[countOfGenerator] = count + 1;
    }

    int countOfNumbersOnLine = 0;
    for (int countOfOutput = 0; countOfOutput < 100; countOfOutput++)
    {
        if (countOfNumbersOnLine == 10)
        {
            System.out.println("");
            countOfNumbersOnLine = 0;
            countOfOutput--;
        }
        else
        {
            if (arrayOfGenerator[countOfOutput] == 10)
            {
                System.out.print(arrayOfGenerator[countOfOutput] + "  ");
                countOfNumbersOnLine++;
            }
            else
            {
                System.out.print(arrayOfGenerator[countOfOutput] + "   ");
                countOfNumbersOnLine++;
            }
        }
    }

    System.out.println("");
    System.out.println("");

    String occurrencesReport = "";
    String graph = "";

    for (int countOfNumbers = 0; countOfNumbers < countOfArray.length; countOfNumbers++)
    {
        occurrencesReport += "The number " + (countOfNumbers + 1) + 
            " occurs " + countOfArray[countOfNumbers] + " times.";

        if (countOfNumbers != 9)
            graph += (countOfNumbers + 1) + "   ";
        else
            graph += (countOfNumbers + 1) + "  ";

        for (int a = 0; a < countOfArray[countOfNumbers]; a++)
        {
            graph += "*";
        }
        occurrencesReport += "\n";
        graph += "\n";
    }

    System.out.println(occurrencesReport);
    System.out.println(graph);

    int max = 0;
    int test = 0;
    for (int counter = 0; counter < countOfArray.length; counter++)
    {
        if (countOfArray[counter] >= max)
        {
            max = countOfArray[counter];
            test = counter + 1;
        }
    }

    System.out.println("The number that appears the most is " + test + ".");

}
}

程序创建一个数组,该数组将存储100个整数(所有这些都在1到10之间),这些整数由随机数生成器生成,然后每行打印出10个这个数组。然后它扫描这些整数,计算每个数字出现的频率并将结果存储在第二个数组中。 在此之后,它会输出一个星号的水平条形图,显示每个数字在最终输出最常出现的数字之前出现的频率。

我以为我完全完成了代码,但我刚刚意识到,如果多个数字出现的次数相同,我的代码的最后一部分无法处理,例如如果数字3和5都出现12次,则代码只能生成其中一个。

有没有人能解决这个问题?

谢谢, 安德鲁

3 个答案:

答案 0 :(得分:0)

我认为这不是某种功课,所以我为你提供了另一种方法。

- 使用Collection代替ArrayList代替Array

- 使用Collections.frequency(Object o)之类的方法来了解Collection值发生的时间

答案 1 :(得分:0)

而不仅仅是做

System.out.println("The number that appears the most is " + test + ".");

再次遍历countOfArray,为每个与max具有相同值的元素执行打印。

答案 2 :(得分:0)

有几种方法可以解决这个问题,范围从快速到复杂。最简单的方法就是这样蛮力:

int max = 0;
//int test = 0;
for (int counter = 0; counter < countOfArray.length; counter++)
{
    if (countOfArray[counter] >= max)
    {
        max = countOfArray[counter];
        //test = counter + 1;
    }
}

System.out.print("The number that appears the most is");
boolean first = true;
for(int i = 0; i < countOfArray.length; i++)
{
    if(countOfArray[i] == max)
    {
        if(first)
            first = false;
        else
            System.out.print(",");
        System.out.print(" " + (i+1) );
    }
}
System.out.println(".");

这是输出:

6   2   6   5   6   8   9   3   5   8   
9   8   10  10  4   5   8   9   8   5   
1   7   8   5   6   7   10  4   5   4   
2   7   9   2   3   3   1   2   10  3   
5   2   10  1   1   6   3   3   8   10  
2   6   10  2   5   1   4   10  8   7   
7   8   7   3   7   8   3   4   5   5   
7   8   9   8   6   6   8   1   10  3   
2   5   4   6   9   9   10  10  1   10  
9   4   10  9   7   3   4   3   2   4   

The number 1 occurs 7 times.
The number 2 occurs 9 times.
The number 3 occurs 11 times.
The number 4 occurs 9 times.
The number 5 occurs 11 times.
The number 6 occurs 9 times.
The number 7 occurs 9 times.
The number 8 occurs 13 times.
The number 9 occurs 9 times.
The number 10 occurs 13 times.

1   *******
2   *********
3   ***********
4   *********
5   ***********
6   *********
7   *********
8   *************
9   *********
10  *************

The number that appears the most is 8, 10.

有更清洁的方法可以解决这个问题,但希望这会给你一个不错的开始!