본문 바로가기
Programming/Android

ArrayList 사용예제

반응형

http://keiis.co.kr/study/020.WebLanguage/500.JAVA/020.CollectionAPI/050.ArrayList/B100.ArrayList_Example_01.html 

                            [ ArrayList 사용예제 ]  

☞ 문자형 ArrayList 예제

package  testAPIs; 
 
import  java.util.*; 

public class  testArrayList { 
    
public static void  main(String[] args) { 
        
// myArrayList를 생성한다. 
        ArrayList<String> myArrayList = 
new ArrayList<String>(); 
         
        
// 아무 데이터가 없을 때 크기를 한번 보자. 
        System.out.println(myArrayList.size()); 
        
// 0이 출력 되었네. 
         
        
// 데이터1이라는 데이터를 집어넣는다. 
        myArrayList.add("
데이터1"); 
        System.out.println(myArrayList.get(0)); 
       
 // 정상적으로 "데이터1"이 출력된다. 
         
        System.out.println(myArrayList.size()); 
        
// 크기는 1 
         
        
// 데이터를 3개 추가하면 전체 크기는 4이겠지. 
        myArrayList.add("
데이터2");
        myArrayList.add("
데이터3"); 
        myArrayList.add("
데이터4"); 
        printList(myArrayList); 
        System.out.println(myArrayList.size()); 
         
        
// 데이터2를 삭제해보고 크기를 재보자. 
        myArrayList.remove(1); 
        printList(myArrayList);
        System.out.println(myArrayList.size()); 
        
// 중간의 것을 삭제해도 전체 크기는 3으로 자동으로 줄어드는구나... 
        System.out.println(myArrayList.get(1)); 
        
// 데이터2의 인덱스 번호에는 데이터3이 나오네 
    } 
     
    
// ArrayList를 인수로 주고 받아서 처리 
    
public static void printList(ArrayList<String> myArrayList){ 
        for (String myStr : myArrayList){ 
            System.
out.println(myStr); 
        } 
    } 
}

 

public ArrayList<ProfileBean> personalList(int start, int end) throws Exception{
     
     Connection conn = 
null;
     PreparedStatement pstmt = 
null;
     ResultSet rs = 
null;
     ArrayList<ProfileBean> list = 
null;
     
     
try{
      conn = conn();
      pstmt = conn.prepareStatement("
SELECT * FROM"+ 
                                    "(SELECT ROWNUM AS rn, a.* FROM"+
                                    "(SELECT * FROM personalinfo ORDER BY personalid ASC ) a)" +
                                    "WHERE rn BETWEEN ? AND ?"
);
      
      System.out.println("
Start : [" + start +" ] "+ "End: [" + end + " ]" );
      
      pstmt.setInt(1,start);
      pstmt.setInt(2, end);
      rs = pstmt.executeQuery();
      
if(rs.next()){
       list = 
new ArrayList<ProfileBean>(end);
       
do{
        ProfileBean pro = 
new ProfileBean();
        pro.setPersonalid(rs.getInt("
personalid"));
        pro.setName(rs.getString("
name"));
        pro.setProjectsite(rs.getString("
projectsite"));
        list.add(pro);
       }
while(rs.next());
      }
     }
catch(Exception ex){
      ex.printStackTrace();
     }
finally{
   if (rs != 
null) try { rs.close(); } catch(SQLException ex) {}
            
if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {}
            
if (conn != null) try { conn.close(); } catch(SQLException ex) {}
     }
     
     
return list;
    }
    

    //==============================================================================

    
public ArrayList<ProfileBean> experienceList(int start, int end) throws Exception{
     
     Connection conn = 
null;
     PreparedStatement pstmt = 
null;
     ResultSet rs = 
null;
     ArrayList<ProfileBean> list = 
null;
     
     
try{
      conn = conn();
      pstmt = conn.prepareStatement(
"SELECT * FROM(" +
                                    "SELECT RUMNM AS rn, a.* FROM(" +
                                    "SELECT * FROM experienceinfo ORDER BY personalid ASC) a)" + 
                                    "WHERE rn BETWEEN ? AND ?"
);
      System.out.println("
Start : [" + start +" ] "+ "End: [" + end + " ]" );
      
     }
catch(Exception ex){
      ex.printStackTrace();
     }
finally{
      
if (rs != null) try { rs.close(); } catch(SQLException ex) {}
            
if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {}
            
if (conn != null) try { conn.close(); } catch(SQLException ex) {}
     }
     
return list;
    }

 

반응형

'Programming > Android' 카테고리의 다른 글

Eclipse 사용시 알아두면 좋은 유용한 단축키 모음  (0) 2010.07.06
안드로이드 UI 툴 DroidDraw  (0) 2010.07.06
안드로이드 개발환경  (0) 2010.07.05
안드로이드 네트워킹 API  (0) 2010.03.31
안드로이드  (0) 2010.03.31