Java 如何找到一个集合的长度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19573403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:26:22  来源:igfitidea点击:

how to find the length of a set?

javaandroidset

提问by user2872261

I'm trying to find the lentgh of a set from a preference. I tired set.length, but it doesnt work. Anyone know another way to find a length of a set?

我试图从偏好中找到一组的长度。我厌倦了 set.length,但它不起作用。有谁知道另一种方法来找到一个集合的长度?

   set = prefs.getStringSet("key", null);
            int X = set.length; //it doesn't like length....

回答by Rahin

I believe instead of set.length, you'll want to call set.size()

我相信而不是 set.length,你会想要打电话 set.size()

回答by Terry

Your set is a Set<String>, not a String[]. So you have to call:

你的集合是一个Set<String>,而不是一个String[]。所以你必须调用:

int x = set.size();