`
Yinny
  • 浏览: 293028 次
  • 性别: Icon_minigender_2
  • 来自: 杭州
社区版块
存档分类
最新评论

JAVA.UTIL.ARRAYLIST 详解

阅读更多
java.util.ArrayList
  数组和数组列表之间有着重大的区别。数组是 Java 语言的一个特征,对于每个元素类型 T ,都有数组类型 T[]; 然而, ArrayList 类是个定义 java.util 包中的类库。这是一个存放 object 类型元素的 " 普通性 " 的类型。要注意的是,要从数组列表中提取元素时,需要进行类型转换。
  使用 add 方法可以向数组列表中添加新元素:
   ArrayList staff = new ArrayList();
   staff.add(new Employee(....));
   staff.add(new Employee(....));
   ArrayList 类管理了一个 Object 引用的内部数组。最终,可能会用完数组的空间。如果调用了 add ,而内部数组已经满了,数组列表将自动创建了一个更大的数组,并自动把小数组中的对象拷贝到大数组中
  Size 方法返回数组列表的实际元素个数
    staff.size()    它等价于数组 a 的 a.length  
    访问数组列表元素
因为 ArrayList 类不是 Java 语言的一部分,它只是个提供在标准库中、由某人编写的工具类,并不像在访问数组元素时可以使用 [] 语法那
样,要存取或改变数组元素,你必须使用 get 和 set 方法
   要设置第 i 个元素,需要使用:
   staff.set(i,harry);
得到数组列表元素要更复杂一些,因为 get 方法返回的类型是 Object ,你还需要把它转换为想要的类型
   Employee e = (Employee)staff.get(i); 它等价于 Employee e = a[i];

   最后总结一下:
  无需指定数组大小
用 add 增添任意多的元素 ;
用 size() 代替 length 计算元素的个数 ;
用 (Employee)a.get(i) 代替 a[i] 访问元素 i;
public  Collection setRollBack(QueueVB job)  throws  CustomException
     {
       
        String sqlselect  =   " select FileName from JobQueue where BatchNum= " + job.getBatchNum() + "  and DocTypeID= " + job.getDocTypeID() + " and JobState='6' " ;
        String sql  =   " update JobQueue set JobState= " + JobState.SAVE + "  where BatchNum= " + job.getBatchNum() + "  and DocTypeID= " + job.getDocTypeID() + "  and JobNum= " + job.getJobNum() + "" ;
         ArrayList array = new  ArrayList();
        System.out.println(sql);
                 try
                 {
                    getConnection = ConnectionLocator.getInstance().getConnection(strJNDI);
                    ResultSet rs = null ;
                     if (getConnection != null )
                     {
                        PreparedStatement pstmt1 = getConnection.prepareStatement(sqlselect);
                        PreparedStatement pstmt = getConnection.prepareStatement(sql);
                       
                        rs  =  pstmt1.executeQuery();
                        pstmt.executeUpdate();
                         while (rs.next())
                         {
                             String fileName  =  rs.getString( " FileName " );
                            FileNameVB filenameVB  =   new  FileNameVB();// 将得到的 “ 文件名 “ 进行封装
                            filenameVB.setFileName(fileName);
                            array.add(filenameVB);
                        }
                         try
                         {
                            pstmt.close();
                        }
                         catch (SQLException e)
                         { throw   new  CustomException(e.getMessage(), " exs " );}
                         try
                         {
                            getConnection.close();
                        }
                         catch (SQLException e)
                         { throw   new  CustomException(e.getMessage(), " exs " );}
                    }
                }
                 catch (SQLException e)
                 { throw   new  CustomException(e.getMessage(), " ex00050 " );}
                 return  array;
    }
读取 arraylist 里的值
Collection m  = inputFacade.setRollBack(job);
                    ArrayList array = (ArrayList)m;
                     for ( int  i=0;i<array.size();i++)
                     {  
                        filenameVB = (FileNameVB)array.get(i);
                           }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics