[Java] ArrayList →→→→→进入此内容的聊天室

来自 , 2020-05-17, 写在 Java, 查看 102 次.
URL http://www.code666.cn/view/704afe07
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3.  
  4. /**
  5.  * @ClassName: ArrayListTest2
  6.  * @Description: TODO(这里用一句话描述这个类的作用)
  7.  * @author Zhouwei
  8.  * @date 2014-4-3 上午9:03:21
  9.  *  
  10.  */
  11. public class ArrayListTest2 {
  12.  
  13.         /**
  14.          * @param args
  15.          */
  16.         public static void main(String[] args) {
  17.                 // TODO Auto-generated method stub
  18.                        
  19.                 Student zhoutao=new Student("周涛",19);
  20.                 Student chenwenjie=new Student("陈文杰",20);
  21.                 Student qiaoxin=new Student("乔鑫",20);
  22.                
  23.                 ArrayList arrList=new ArrayList();
  24.                 arrList.add(zhoutao);
  25.                 arrList.add(chenwenjie);
  26.                 arrList.add(qiaoxin);
  27.                
  28.                 //查询name属性="陈文杰 " 的student 对象
  29.                
  30.                 for(int i=0;i<arrList.size();i++){
  31.                         //取出元素,需要强转
  32.                      Student s=(Student)arrList.get(i);
  33.                      
  34.                      if(s.getName().equals("陈文杰")){
  35.                          System.out.println(s);
  36.                      }
  37.                  
  38.                 }
  39.                
  40.                
  41.                 //泛型集合  ArraList<元素类型>
  42.                 ArrayList<Student> list=new ArrayList<Student>();
  43.                 list.add(zhoutao);
  44.                 list.add(chenwenjie);
  45.                 list.add(qiaoxin);
  46.                
  47.                 //泛型集合的好处,取元素不需强转
  48.                 Student s=list.get(0);
  49.                
  50.                 HashMap<String, Student> map=new HashMap<String, Student>();
  51.                
  52.                
  53.                
  54.         }
  55.  
  56. }
  57.  

回复 "ArrayList"

这儿你可以回复上面这条便签

captcha