隨筆-11  評論-5  文章-1  trackbacks-0

          ?? 數(shù)組和數(shù)組列表之間有著重大的區(qū)別。數(shù)組是Java語言的一個(gè)特征,對于每個(gè)元素類型T,都有數(shù)組類型T[];然而,ArrayList類是個(gè)定義java.util包中的類庫。這是一個(gè)存放object類型元素的"普通性"的類型。要注意的是,要從數(shù)組列表中提取元素時(shí),需要進(jìn)行類型轉(zhuǎn)換。
          ? 使用add方法可以向數(shù)組列表中添加新元素:
          ?? ArrayList staff = new ArrayList();
          ?? staff.add(new Employee(....));
          ?? staff.add(new Employee(....));
          ?? ArrayList類管理了一個(gè)Object引用的內(nèi)部數(shù)組。最終,可能會用完數(shù)組的空間。如果調(diào)用了add,而內(nèi)部數(shù)組已經(jīng)滿了,數(shù)組列表將自動創(chuàng)建了一個(gè)更大的數(shù)組,并自動把小數(shù)組中的對象拷貝到大數(shù)組中
          ? Size方法返回?cái)?shù)組列表的實(shí)際元素個(gè)數(shù)
          ??? staff.size()??? 它等價(jià)于數(shù)組a的a.length???
          ???訪問數(shù)組列表元素
          因?yàn)?ArrayList類不是Java語言的一部分,它只是個(gè)提供在標(biāo)準(zhǔn)庫中、由某人編寫的工具類,并不像在訪問數(shù)組元素時(shí)可以使用[]語法那

          樣,要存取或改變數(shù)組元素,你必須使用get和set方法
          ?? 要設(shè)置第i個(gè)元素,需要使用:
          ?? staff.set(i,harry);
          得到數(shù)組列表元素要更復(fù)雜一些,因?yàn)間et方法返回的類型是Object,你還需要把它轉(zhuǎn)換為想要的類型
          ?? Employee e = (Employee)staff.get(i); 它等價(jià)于Employee e = a[i];

          ?? 最后總結(jié)一下:
          ?無需指定數(shù)組大小
          用add增添任意多的元素;
          用size()代替length計(jì)算元素的個(gè)數(shù);
          用(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();//將得到的“文件名“進(jìn)行封裝
          ????????????????????????????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);
          posted on 2006-09-16 10:28 Crespo 閱讀(1191) 評論(1)  編輯  收藏 所屬分類: Java web

          評論:
          # re: java.util.ArrayList 2006-09-25 10:04 | sunyi
          hahahah

          u just do a such professional blog
          can't understand
          huhuhuhuhu


          best wishes always
            回復(fù)  更多評論
            
          主站蜘蛛池模板: 田林县| 金川县| 兴和县| 孟村| 峡江县| 台南市| 凉城县| 深州市| 太谷县| 石家庄市| 永昌县| 泽普县| 花莲市| 南和县| 日喀则市| 鹤峰县| 涿鹿县| 潢川县| 山西省| 夏津县| 广河县| 闵行区| 富顺县| 斗六市| 龙海市| 普安县| 霍林郭勒市| 赤峰市| 丰顺县| 阳山县| 罗甸县| 金堂县| 深水埗区| 沂南县| 海林市| 温州市| 浙江省| 察哈| 防城港市| 京山县| 长沙市|