您的位置:首页 > 其它

实现统计数组中相同元素个数并输出

2007-08-28 20:08 260 查看
package exercise;
import java.util.*;

public class TestRepeatNumber {

TreeSet set = new TreeSet();
TreeMap map = new TreeMap();

ArrayList list = new ArrayList();
int[] num={5,7,8,5,10,8,5,35};

public void init()
{
for(int i=0;i<num.length;i++)
{
list.add(new Integer(num[i]));
set.add(new Integer(num[i]));

}
list.trimToSize() ;

System.out.println(list);
System.out.println(set);
sort();
}

public void sort()
{
int count=0;
Iterator it = set.iterator();
while(it.hasNext())
{

Object o = it.next();
count=Collections.frequency(list, o);
System.out.println(o+" "+ count);
map.put(o, count);
}

System.out.println(map.toString());
}

public static void main(String[] args) {

TestRepeatNumber tn = new TestRepeatNumber();
tn.init();
}

}
这是实现数组里有重复数字,并且将重复的数字输出
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐