ちょうきょう666

导航

java集合

Map集合的两种遍历方式:Map集合无序

public class Map1 {
public static void main(String[] args) {
 Map<String, String> map=new HashMap();
 map.put("12", "ss");
 map.put("13", "sz");
 map.put("11", "tg");
 map.put("90", "sp");
 //遍历
 Set<String> set=map.keySet();
 for(String key:set) {
  System.out.println(key+"------------"+map.get(key));
 }
 //遍历2
 Set<Map.Entry<String, String>> set2=map.entrySet();
 for(Map.Entry<String, String> sss:set2) {
  System.out.println(sss.getKey()+"------"+sss.getValue());
 }
}
}

LIst集合的增删查,有序

public class Demo7 {
 
 public static void main(String[] args) {
  //添加元素
  ArrayList als=new ArrayList();
  Student s1=new Student("12","XZG");
  Student s2=new Student("15","fx");
  Student s3=new Student("17","ff");
  Student s4=new Student("23","oo");
  Student s5=new Student("68","yy");
  als.add(s1);
  als.add(s2);
  als.add(s3);
  als.add(s4);
  als.add(s5);
  System.out.println("元素个数:"+als.size());
  System.out.println(als.toString());
  //删除元素
  als.remove(0);
  System.out.println("元素个数"+als.size());
  //遍历元素
  Iterator it= als.iterator();
  while(it.hasNext()) {
   Student st=(Student)it.next();
   System.out.println(st.toString());
  }
  ListIterator lit=als.listIterator();
  while(lit.hasNext()) {
   Student st=(Student)lit.next();
   System.out.println(st.toString());
  }
  System.out.println(als.contains(s2));//判断此元素是否存在
  System.out.println(als.isEmpty());//判断集合是否为空
  
  
 }
}

posted on 2020-05-29 11:01  ちょうきょう666  阅读(118)  评论(0编辑  收藏  举报