我的主要目的是获得这种输出:它显示数值和它在数组中出现的次数。下面是一个示例,但在代码中,我将要求用户将数据整数输入到数组中
e.g。对于数组:{ - 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
答案 0 :(得分:0)
您递增number
以尝试计算当前array[i]
被查看的次数,但您从未将其值重置为0.
此外,在方法结束时,您只打印N Count
表的一行。如果要为索引的每个唯一元素打印一行,是否需要打印多行?
有一种更简单的方法来计算数组中不需要对其进行排序的元素的出现 - 提示,考虑使用Map<Integer, Integer>
。
答案 1 :(得分: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
答案 2 :(得分:0)
我要做的是创建一个具有两个实例(值和计数)的新节点类。每次遇到新数字时,使用其值创建一个新节点,并将其计数增加1。拥有节点列表并将节点添加到此列表中。对于下一个输入,请检查以前是否已经看到该值,例如
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和swapping(冒泡排序,但有很多方法可以排序)对列表进行排序。
之后只需打印值及其计数
答案 3 :(得分:-1)
如果这不是一个如何使用数组的课程,我强烈主张与List和其他集合进行联系 - 但最好是List,具体来说就是ArrayList。太方便了!这很容易。
有3或4个基本操作:构造函数定义List,添加元素,删除元素,迭代所有元素。
还有大约50种其他不常用的方法,以及使用Lists等的方法。
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很快,相信它 - 不 - 不要相信它,测试它!