如何查找数组列表中不同项目的总数。 [英] How to find total number of different items within an arraylist.
本文介绍了如何查找数组列表中不同项目的总数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法迭代列表来确定不同类型的总数
ie。
ArrayList< String> Store = new ArrayList< String>();
this.Store.add(Gum);
this.Store.add(Gum);
this.Store.add(Socks);
this.Store.add(糖果);
该列表共有4个项目,但只有三种不同类型的项目(Gum,Sucks,Candy)
如何设计一种方法来计算3?
解决方案
p>什么 Bhesh Gurung说,但在代码中:
int numUnique = new HashSet< String>(Store).size();
如果您实际拥有的是 StoreItems
需要通过 getName()
然后我会做
Set<串GT; itemNames = new HashSet< String>(); (storeItem item:Store)
itemNames.add(item.getName());
int numUnique = itemNames.size();
I've done some searching but I wasn't able to find a valid solution. I have an arraylist storing Strings such as gum, socks, OJ, dog food...
I am having trouble iterating the list to determine the total number of differnt types of items.
ie.
ArrayList<String> Store = new ArrayList<String>();
this.Store.add("Gum");
this.Store.add("Gum");
this.Store.add("Socks");
this.Store.add("Candy");
The list has 4 total items, but only three different kinds of items (Gum, Sucks, Candy).
How would I design a method to calculate the 3?
解决方案
What Bhesh Gurung said, but in code:
int numUnique = new HashSet<String>(Store).size();
If what you actually have is StoreItems
and need to go through getName()
then I would do
Set<String> itemNames = new HashSet<String>();
for (StoreItem item : Store)
itemNames.add(item.getName());
int numUnique = itemNames.size();
这篇关于如何查找数组列表中不同项目的总数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文