一、Set
特点:三无产品 无序 无下标 无重复
public class Demo1 {
public static void main(String[] args) {
//set特点:三无产品 无序 无下标 不能重复
Set<String> set=new HashSet<String>();
set.add("zs");
set.add("ls");
set.add("ww");
set.add("zs");//增加不进去
set.add("mz");
set.add("zl");
System.out.println(set.size());
//输出结果为5 符合set集合中无重复的特点
}
}
遍历方式:除for循环都可遍历(foreach遍历或Iterator遍历);
//遍历方式:除for循环以外都可以遍历 因为没有下标
//第一种遍历方式:
System.out.println("foreach遍历~~~~~");
for (String s: set) {
System.out.println(s);
}
//迭代器遍历
System.out.println("迭代器遍历~~~~~~~");
Iterator<String>it=set.iterator();
while(it.hasNext()) {//是否有下一个值
String str=it.next();//自动移动到下一个值
System.out.println(str);
}
//输出结果都为:ww zl ls zs mz 恰巧符合了Set特点中无序这一说法;