您好,欢迎访问一九零五行业门户网

Java 实例 - 集合长度

以下实例演示了如何使用 collections 类 的collection.add() 来添加数据并使用 collection.size()来计算集合的长度:
/* author by w3cschool.cc main.java */import java.util.*;public class main { public static void main(string [] args) { system.out.println( "集合实例!\n" ); int size; hashset collection = new hashset (); string str1 = "yellow", str2 = "white", str3 = "green", str4 = "blue"; iterator iterator; collection.add(str1); collection.add(str2); collection.add(str3); collection.add(str4); system.out.print("集合数据: "); iterator = collection.iterator(); while (iterator.hasnext()){ system.out.print(iterator.next() + " "); } system.out.println(); size = collection.size(); if (collection.isempty()){ system.out.println("集合是空的"); } else{ system.out.println( "集合长度: " + size); } system.out.println(); }}
以上代码运行输出结果为:
集合实例! 集合数据: white yellow blue green 集合长度: 4
以上就是java 实例 - 集合长度的内容。
其它类似信息

推荐信息