首页 >> 民生评论

list中如何判断相同值的个数节能

民生评论  2020-10-21 06:48 字号: 大 中 小

list中如何判断相同值的个数?

比如说一个list,里面是1,1,2,2,3,3,3,4,4

如何得到1有2个,2有2个,3有3个,4有2个?

我有个笨办法。

比如你已经知道list里的数就之间,就建个数组,假设叫count[10]

然后,count[list里的数]++;

如果list里的数个数未知,也是这么个意思,就是麻烦点。

List Integer list = new ArrayList Integer();

Map Integer,Integer map = new HashMap Integer,Integer();

for(Integer i :list){

t(i, t(i)+1);

}

List Integer list = new ArrayList Integer();

Map Integer,Integer map = new HashMap Integer,Integer();

for(Integer i :list){

t(i, t(i)+1);

}

不一定用Integer,可以改为任意object

List list = new ArrayList(); // 里面是1,1,2,2,3,3,3,4,4

Map Integer,Object map = new HashMap Integer,Object();

for(Object i : list){

t(i, t(i)+1);

}

你先把你的数组赋值给另一个数组,两个数组对比,只要“下标”不一样,并且相等的,就找出来。

你先把你的数组赋值给另一个数组,两个数组对比,只要“下标”不一样,并且相等的,就找出来。

Java code

package hers;

import rayList;

import llections;

import st;

import anner;

/**

* 计算字符串的字符出现的次数

* @author lichaoever

*

*/

public class getCount {

ListString list据台湾中广报道 = new ArrayListString();

Scanner s = new Scanner();

public getCount() {

intln("请输入字符:");

unt(xtLine());

}

private void count(String str)

{

char[] c = CharArray();

for(char s : c)

{

d(lueOf(s));

}

rt(list);

for(String s : list)

{

int begin = dexOf(s);

int end = stIndexOf(s);

intln("字母'"+ s + "'有:"+(end - begin + 1)+"个");

}

}

public static void main(String[] args) {

new getCount();

}

}

Java code import rayList;import st;public class B { public static void main(String[] args) { ListInteger list = initData(); int x = ze(); while(ze()!=0){ ListInteger arr = list; Integer obj = t(0); ListInteger l1 = new ArrayListInteger(); d(obj); moveAll(l1); intln(obj+"出现"+(ze())+"次"); x = ze(); } } public static ListInteger initData(){ ListInteger list = new ArrayListInteger一起将小孩送到医院检查。拍片结果显示(); d(1); d(1); d(2); d(2); d(3); d(3); d(3); d(4); d(4); return list; }}

治疗肝硬化的药物怎么选
1岁孩子积食怎么办
本溪妇科医院
推荐资讯