ArrayList, HashMap의 Call By Value, Call By Reference 참조변수 테스트



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import java.util.ArrayList;
import java.util.HashMap;
 
import sun.text.CompactShortArray.Iterator;
 
 
public class AddressTest {
    
    public static class CallByValue{
        public int a;
    }
 
    public static void CallByReference(CallByValue param){
        param.a = 100;
    }
    
    public static void main(String[] args) {
        
        // ---------------------------------- #case1----------------------------------------------
        
        ArrayList<Integer> myArrayList1=new ArrayList<Integer>();
        ArrayList<Integer> myArrayList2=new ArrayList<Integer>();
        
        myArrayList1.add(10);
        myArrayList1.add(20);
        myArrayList1.add(30);
        // myArrayList1 = 10,20,30
 
        myArrayList2 = myArrayList1;
        
        myArrayList2.add(40);
        myArrayList2.add(0,5);
        myArrayList1.add(50);
        // myArrayList2 = 5,10,20,30,40
        
        System.out.println("myArrayList1값: "+myArrayList1); // 5,10,20,30,40
        System.out.println("myArrayList2값: "+myArrayList2); // 5,10,20,30,40
        
        // ---------------------------------- #case2----------------------------------------------
        
        HashMap<Integer, String> myHashMap1 = new HashMap<Integer, String>();
        HashMap<Integer, String> myHashMap2 = new HashMap<Integer, String>();
 
        myHashMap1.put(3"사이다");
        myHashMap1.put(1"커피");
        myHashMap1.put(2"우유");
        System.out.println("myHashMap1값: "+myHashMap1); // myHashMap1값: {3=사이다, 2=우유, 1=커피}
        myHashMap2 = myHashMap1;
        
        myHashMap2.put(4,"콜라");
        myHashMap2.put(5,"보리차");
        
        System.out.println("myHashMap1값: "+myHashMap1); // myHashMap1값: {5=보리차, 4=콜라, 3=사이다, 2=우유, 1=커피}
        System.out.println("myHashMap2값: "+myHashMap2); // myHashMap2값: {5=보리차, 4=콜라, 3=사이다, 2=우유, 1=커피}
        System.out.println(myHashMap2.keySet()); // [5, 4, 3, 2, 1]
        
        java.util.Iterator<Integer> myIterator = myHashMap2.keySet().iterator();
        while(myIterator.hasNext()){
            Integer key = myIterator.next();
            System.out.println(key+myHashMap2.get(key));
        }
        
        // ---------------------------------- #case3----------------------------------------------
        
        int a = 1;
        int b;
        
        b = a;
        b = 10;
        
        System.out.println("a값: "+a); // a = 1
        System.out.println("b값: "+b); // b = 10
        
        
        // ---------------------------------- #case4----------------------------------------------
        
        String c = new String();
        String d = new String();
        
        c = "ccc" ; 
        d = c ;
        d = "ddd";
        
        System.out.println("c값: " + c); // c = ccc
        System.out.println("d값: " + d); // d = ddd
        
        // ---------------------------------- #case5----------------------------------------------
        
        CallByValue callbyValue1 = new CallByValue();    
        callbyValue1.a = 200;
        System.out.println("callbyValue1값: "+callbyValue1.a); // 200
        CallByReference(callbyValue1);
        System.out.println("callbyValue1값: "+callbyValue1.a); // 100
    }
 
}
cs


case 1을 보면 myArrayList1과 myArrayList2는 서로 다른 변수로 선언 되어 있고


myArrayList1의 값을 add 해주고 myArrayList2 = myArrayList1 로 myArrayList1의 주소를 참조하고 있다.


그리고 myArrayList2의 값을 add해주고 myArrayList1값과 myArrayList2값을 찍어주면


같은 값(5,10,20,30,40) 이 출력된다.



case 2도 HashMap을 사용했는데 myHashMap2 = myHashMap1 로 주소를 참조해


myHashMap1과 myHashMap2의 같은 값{5=보리차, 4=콜라, 3=사이다, 2=우유, 1=커피}이 출력된다.




case 3case 4 call by value 로 객체를 생성해 값을 집어넣거나 값을 직접적으로 넣어도 서로 다른 값이 나오게 된다.



case 5는 메소드를 타게하여 주소로 접근해 값을 변경시키고 있다.



작업 방법


1. 해쉬맵 형태 Entity > ArrayList로 변환 


2. ArrayList에서 필요한 값에 대한 키에 접근 > 적절한 분기 처리하여 필요한 값에 대한 데이터를 추출 


3. 추출한 데이터를 다시 해쉬맵형태의 Entity에 키, 밸류로 넣음


4. 해쉬맵 형태의 엔티티를 ArrayList에 추가 > ArrayList를 해쉬맵 형태의 엔티티에 넣음



해쉬맵 형태로 들어가있는 Entity에서 ArrayList로 형 변환하여(ArrayList myArrList1 = EntityUtil.ResultSetToList(rslt);) 

담겨있는 데이터는 총 2개이다.


이 ArrayList 파람값에 접근해 값을 추출하여 새로운 키값을 만들어 값을 넣어주고 싶다.


1
2
3
4
5
6
7
8
9
10
[
    {reply_yn=N, inquiry_seq=265194, faq_ty1_nm=배송, mall_faq_ty_nm=매장상품, proc_dt=0.0, title_txt=테스트3, cancel_yn=N, user_nm=이상현, reply_ready=N, ord_store_nm=null, inquiry_dt2=2017-09-06 15:45:04, store_id=0075, inquiry_dt=2017-09-06, oos_yn=N, my_mall_faq_ty=63300, ord_store_id=null, user_id=20901762, r_num=1, faq_ty2_nm=미배송/수량부족, inquiry_kind=G, rownum=1, inquiry_txt=1234, reply_sms_update_dt=null, mod_dt=2017-09-06, basicreply_team=null, faq_ty1=63310, rating_cd=Brand-New, modstaff=null, ord_ty=인터넷, ord_no=100012934, mod_dt2=2017-09-06 15:45:04, reply_sms_yn=N, store_nm=잠실점, inquiry_img=null}
    {reply_yn=N, inquiry_seq=265193, faq_ty1_nm=주문, mall_faq_ty_nm=공통/기타, proc_dt=0.0, title_txt=테스트1, cancel_yn=N, user_nm=이상현, reply_ready=N, ord_store_nm=null, inquiry_dt2=2017-09-06 15:44:25, store_id=0075, inquiry_dt=2017-09-06, oos_yn=N
        , mall_ord_info= [
                        {"mart_style_type":"19030","god_nm":"프리미엄 물티슈 70매 캡형 10팩","mart_style_name":"[택배상품]","god_cnt":"0","trade_date":"2017.08.31","store_nm":"잠실점","ord_st":"결제완료","ord_no":"100011806"}
                       ,{"mart_style_type":"19020","god_nm":"[레고\/씨티(CITY)] 소방트럭과 보트 - 7213_레고코리아정품_BASIC \/ 1","mart_style_name":"[택배상품]","god_cnt":"0","trade_date":"2017.08.30","store_nm":"잠실점","ord_st":"상품준비중","ord_no":"100011660"}
                       ,{"mart_style_type":"19030","god_nm":"[순둥이]PC-01\/프리미엄 무향 플레인 물티슈 70매 캡형 10팩_BASIC \/ 1","mart_style_name":"[택배상품]","god_cnt":"0","trade_date":"2017.08.28","store_nm":"잠실점","ord_st":"주문취소","ord_no":"100011329"}
                 ]
        , my_mall_faq_ty=64100, ord_store_id=null, user_id=20901762, r_num=2, faq_ty2_nm=픽업/퀵배송 주문문의, inquiry_kind=G, rownum=2, inquiry_txt=테스트22, reply_sms_update_dt=null, mod_dt=2017-09-06, basicreply_team=null, faq_ty1=64140, rating_cd=Brand-New, modstaff=null, ord_ty=인터넷, ord_no=100011660, mod_dt2=2017-09-06 15:44:25, reply_sms_yn=N, store_nm=잠실점, inquiry_img=null}
]
cs




데이터가 저장되어있는 ArrayList의 이름은 myArrList1 이다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public Entity selectInquiryExcelList(UserConnection conn, Entity param) throws SQLException {
    ArrayList myArrList1 = EntityUtil.ResultSetToList(rslt); // ArrayList 저장됨.
    ArrayList  queryArrayResult = new ArrayList();
    
    String mall_ord_info;
    String myData;
    
    for (int i = 0; i < myArrList1.size(); i++) { // ArrayList 크기만큼 돌리고
        Entity myArrListInfoEntity = new Entity();
        myArrListInfoEntity = (Entity)myArrList1.get(i); // ArrayList[0~N]  
        myData = myArrListInfoEntity.getString("reply_yn"); //  myData = ArrayList[0~N].reply_yn value(Y or N)
        mall_ord_info = myArrListInfoEntity.getString("mall_ord_info");  // mall_ord_info 컬럼 접근해서
        if(myData == 'N'){
            if(mall_ord_info.length() > 0){ // mall_ord_info 컬럼 안 Array 크기만큼 반복
                if(mall_ord_info.contains("store_nm")){
                    int myint1 =  mall_ord_info.indexOf("store_nm\":\"");
                    myint1 = myint1 + 11;
                    int myint2 =  mall_ord_info.indexOf("\",\"ord_st");
                    String myStringValue1 = mall_ord_info.substring(myint1,myint2); // 주문점포명에 대한 정보 얻어냄
                    myArrListInfoEntity.put("my_ord_info_store", myStringValue1); // 뽑아낸 주문점포명에 대한 데이터를 Entity안에 다시 해쉬맵 형태의 Key, Value 형태로 넣어줌
                }                      
            }                    
        }
        queryArrayResult.add(myArrListInfoEntity); // add를 사용해 Entity 형태의 해쉬맵 데이터를 다시 ArrayList에 넣어줌.
        // 기존의 데이터에서 키와 밸류를 추가해 들어가게 된다.
    }
    _DATA.put("_DATA" , queryArrayResult);
    return _DATA
}
cs



ArrayList 크기만큼 반복하며 돌며 한 row안에 있는 값을 적절히 분기처리하여 indexOf와 substring을 사용해

값을 추출하고 추출한 데이터를 새로운 키값인 "my_ord_info_store" 의 Value로 넣어주고 있다.

해당 반복문이 종료되면 ArrayList 안에

my_ord_info_store 라는 Key로 추가되어 재정의된 ArrayList가 나오게 된다.


1
2
3
4
5
6
7
8
9
10
[
    {reply_yn=N, my_ord_info_store=잠실점, inquiry_seq=265194, faq_ty1_nm=배송, mall_faq_ty_nm=매장상품, proc_dt=0.0, title_txt=테스트3, cancel_yn=N, user_nm=이상현, reply_ready=N, ord_store_nm=null, inquiry_dt2=2017-09-06 15:45:04, store_id=0075, inquiry_dt=2017-09-06, oos_yn=N, my_mall_faq_ty=63300, ord_store_id=null, user_id=20901762, r_num=1, faq_ty2_nm=미배송/수량부족, inquiry_kind=G, rownum=1, inquiry_txt=1234, reply_sms_update_dt=null, mod_dt=2017-09-06, basicreply_team=null, faq_ty1=63310, rating_cd=Brand-New, modstaff=null, ord_ty=인터넷, ord_no=100012934, mod_dt2=2017-09-06 15:45:04, reply_sms_yn=N, store_nm=잠실점, inquiry_img=null}
    {reply_yn=N, my_ord_info_store=잠실점, inquiry_seq=265193, faq_ty1_nm=주문, mall_faq_ty_nm=공통/기타, proc_dt=0.0, title_txt=테스트1, cancel_yn=N, user_nm=이상현, reply_ready=N, ord_store_nm=null, inquiry_dt2=2017-09-06 15:44:25, store_id=0075, inquiry_dt=2017-09-06, oos_yn=N
        , mall_ord_info= [
                        {"mart_style_type":"19030","god_nm":"프리미엄 물티슈 70매 캡형 10팩","mart_style_name":"[택배상품]","god_cnt":"0","trade_date":"2017.08.31","store_nm":"잠실점","ord_st":"결제완료","ord_no":"100011806"}
                       ,{"mart_style_type":"19020","god_nm":"[레고\/씨티(CITY)] 소방트럭과 보트 - 7213_레고코리아정품_BASIC \/ 1","mart_style_name":"[택배상품]","god_cnt":"0","trade_date":"2017.08.30","store_nm":"잠실점","ord_st":"상품준비중","ord_no":"100011660"}
                       ,{"mart_style_type":"19030","god_nm":"[순둥이]PC-01\/프리미엄 무향 플레인 물티슈 70매 캡형 10팩_BASIC \/ 1","mart_style_name":"[택배상품]","god_cnt":"0","trade_date":"2017.08.28","store_nm":"잠실점","ord_st":"주문취소","ord_no":"100011329"}
                 ]
        , my_mall_faq_ty=64100, ord_store_id=null, user_id=20901762, r_num=2, faq_ty2_nm=픽업/퀵배송 주문문의, inquiry_kind=G, rownum=2, inquiry_txt=테스트22, reply_sms_update_dt=null, mod_dt=2017-09-06, basicreply_team=null, faq_ty1=64140, rating_cd=Brand-New, modstaff=null, ord_ty=인터넷, ord_no=100011660, mod_dt2=2017-09-06 15:44:25, reply_sms_yn=N, store_nm=잠실점, inquiry_img=null}
]
cs



+ Recent posts