0

我的主要目的是获得这种输出:它显示值和它在数组中出现的次数。下面是一个示例,但在代码中,我会要求用户将数据整数输入到数组中

例如对于数组: {-12, 3, -12, 4, 1, 1, -12, 1, -1, 1, 2, 3, 4, 2, 3, -12} 输出应该是:

 N  Count
 4  2
 3  3
 2  2
 1  4
-1  1
-12 4

下面是我自己的尝试,但由于某种原因,我无法存储要存储的数组,并在代码的其他部分使用:

import java.util.*;
public class Q4 
{    
    /**
     * @param args
     */
    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub
        int[] myarray = new int[50];

        System.out.println("Enter integers into the system, to quit enter -99");
        Scanner scan=new Scanner(System.in);

        for(int i = 0; i<myarray.length; i++)
        {
            int temp =scan.nextInt();
            if(temp!=(-99))
            {   
                myarray[i]=temp;
            }
            if(temp ==(-99))
            {
                System.out.println("Successfully terminated by inputting -99");
                System.out.println();
                break;
            }
            else if(i==(myarray.length-1))
            {
                System.out.println("successfully filled up array fully");
                System.out.println();
            }   
        }

        for(int i = 0; i<myarray.length; i++)
        {
            System.out.print(myarray[i]+",");
        }
        System.out.print("}");

        int temp=0;
        int number = 0;

        Arrays.sort(myarray);
        System.out.println("Array list: {");
        for (int i = 0; i < myarray.length; i++)
        {
            if(temp==0)
            {
                temp=myarray[i];
                number++;
            }
            else if (temp!=0)
            {
                if (temp==myarray[i])
                {
                    number++;
                }
                else
                {
                    temp=0;
                }
            }
        }
        System.out.print("}");  
        System.out.println();   
        System.out.println();   
        System.out.println("N"+"\t"+"\t"+"Count");
        System.out.println(temp+"\t"+"\t"+number);      
    }
}

这是我的输出,这不是我想要的,

Enter integers into the system, to quit enter -99
12
3123
3123
11
22
-99
Successfully terminated by inputting -99

Array list: {12,3123,3123,11,22,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}Array list: {
}

N       Count
3123        48
4

4 回答 4

0

我要做的是创建一个具有两个实例的新节点类,即值和计数。每次遇到新数字时,使用其值创建一个新节点,并将其计数加一。有一个节点列表并将节点添加到此列表中。对于下一个输入,循环检查该值是否之前已经看过,例如。

for i = 0; i <list.size; i++
     if list.get(i).data == value // if it finds the value increment and break
            list.get(i).count++
            break;
     else if i==list.size-1//if it went through the list and didn't find the value, make a new node of the value and add it to the list
            make a new node
            add it to the list

终止后,通过比较 list.get(i).values 和交换对列表进行排序(冒泡排序浮现在脑海中,但有很多排序方法)

之后只需打印值即可

于 2012-05-12T21:26:20.330 回答
0

您递增number以尝试计算当前array[i]出现的次数,但您从未将其值重置为 0。

N Count此外,在您的方法结束时,您只打印表格的一行。如果要为索引的每个唯一元素打印一行,您不需要打印多行吗?

有一种更简单的方法可以计算不需要排序的数组中元素的出现次数 - 提示,考虑使用Map<Integer, Integer>.

于 2012-05-10T15:52:49.243 回答
0

你应该尝试以下事情。

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Scanner;

public class NumberRepetion {

    public static void main(String[] args) {
        int[] myarray = new int[50];

        System.out.println("Enter integers into the system, to quit enter -99");
        Scanner scan = new Scanner(System.in);

        ArrayList<Integer> myarrList = new ArrayList<Integer>();

        while (scan.hasNext()) {

            int temp = scan.nextInt();
            if (temp != (-99)) {

                // myarray[i] = temp;
                myarrList.add(temp);
            }
            if (temp == (-99)) {
                System.out.println("Successfully terminated by inputting -99");
                System.out.println();
                break;
            }

        }

        Hashtable<Integer, Integer> result = new Hashtable<Integer, Integer>();

        System.out.print("Input Values  {");

        int currIndex = 0 ;
        for (Integer val : myarrList) {
            if (currIndex == ( myarrList.size() - 1 )){
                System.out.print(val);
            }else{
                System.out.print(val + ", ");
            }
            currIndex++ ;
            int currVal = val;
            Integer integer = result.get(currVal);
            if (integer == null || integer == 0) {
                result.put(currVal, 1);
            } else {
                result.put(currVal, ++integer);
            }
        }
        System.out.print("}");
        System.out.println()

        Enumeration<Integer> keys = result.keys();
        System.out.println("N\t\tCount");

        while(keys.hasMoreElements()){
            System.out.println(" " +  keys.nextElement() +"\t\t" + result.get(keys.nextElement()));
        }

        //System.out.println("\n\n\n Result  " + result);

    }

}

输出

Enter integers into the system, to quit enter -99
5
6
5
8
4
-99
Successfully terminated by inputting -99

Input Values    {5, 6, 5, 8, 4}
N       Count
 8      1
 5      1
于 2012-05-10T16:02:23.297 回答
-1

如果这不是关于如何使用数组的课程,我强烈建议与 List 和其他集合联系 - 但最好是 List,具体来说是 ArrayList。太方便了!这很容易。

有 3 或 4 种基本操作:构造函数来定义 List、添加元素、删除元素、迭代所有元素。

还有大约 50 种其他不常用的方法,以及使用列表的方法等等。

public static void main (String [] args)
{
    List <Integer> myarray = new ArrayList <Integer> ();
    System.out.println ("Enter integers into the system, to quit enter -99");
    Scanner scan = new Scanner (System.in);

    while (scan.hasNextInt ())
    {
        int temp = scan.nextInt ();
        if (temp == -99)
        {
            System.out.println ("Successfully terminated by inputting -99");
            System.out.println ();
            break;
        }
        else {
            myarray.add (temp);
            if (myarray.size () == 50)
            {
                System.out.println ("successfully filled array fully up");
                System.out.println ();
            }
        }
    }
    for (int i : myarray)
    {
        System.out.print (i + ",");
    }
    System.out.print ("}");

    Set <Integer> hsi = new HashSet <Integer> (); 
    hsi.addAll (myarray);

    Collections.sort (myarray);
    System.out.println ("Array list: {");
    int idx = 0;
    for (int i: hsi) {
        System.out.println (i + "\t" + Collections.frequency (myarray, i));
    }
    System.out.println (myarray.size ());
}

看看有多简短?只需添加元素 - 您无需提前知道它包含多少元素。没有标记字段或外部值来标记必要的结束!

用法:

java Numbers
Enter integers into the system, to quit enter -99
4 44 0 33 2 2 7 9 1  4 3 90 -99 
Successfully terminated by inputting -99

4,44,0,33,2,2,7,9,1,4,3,90,}Array list: {
0   1
1   1
2   2
3   1
33  1
4   2
7   1
9   1
44  1
90  1
12

您收集值的第一个想法,您喜欢通过索引获取或想要迭代,应该是 ArrayList,而不是普通的旧数组。数组仅在特殊情况下才有用 - 当您确定提前知道大小时。

ArrayLists 很快,相信它 - 不 - 不要相信它,测试它!

于 2012-05-12T23:25:05.330 回答