简体   繁体   中英

2D array representation using Arraylist

I would like to implement a 2D representation of data where each of the row represents features which are in gaussian distribution. I did the following:

     public ArrayList<ArrayList<Double>> getFeature( BufferedReader buffer1)

  {   ArrayList<Double> arrList1 = new ArrayList<Double>();
    ArrayList<ArrayList<Double>> arrList2 = new ArrayList<ArrayList<Double>>();
    String[] strArr = getWords(buff1); 
    for(int j=0 ; j< strArr.length; j++){
    for(int i =0 ;i < 50 ; i++){
        arrList1.add(rand.nextGaussian());
    }
    arrList2.add(arrList1);
    }
    System.out.println("size of arrList2:"+ arrList2.size());       
    System.out.println(arrList2.toArray()[0]);
    System.out.println(arrList2.toArray()[1]);
    return arrList2;
}

When i compile and wants to get the value of each of the row, i get weired output. Can anybody tell me what can be the reason ?

The expected output is:

  System.out.println(arrList2.toArray()[0]);

 [0.08,1.1 .............,0.98]

An array of dimension 50 filled with the value as above.

The following is the weired output. It was too big

[0.46462618324716254, -0.7461971643031985, 0.11198107553318244, -0.5445576195764561, -0.09170153174710902, 1.0599085675813804, -0.5121868425685772, 0.6578194527724543, 0.07180836719554716, -0.8711795090831688, -1.1685999674955914, -0.8467566589850426, -0.9464559617558149, -0.25455126102711567, 0.6880497852836619, 0.05802417861623583, 0.7605219159096197, 0.5573206594413265, -0.6464485036951102, 0.9928577372480463, 0.769468353755745, -1.0349221359588956, 1.7475686681406222, 0.7286721770852165, 1.1938115793074722, 0.05051321258899094, -1.5274223735062666, -0.10652710930319426, -2.1566969444968622, 1.4572264331572156, -0.07630289414615696, 0.2776127747056551, -0.3009457462994894, 0.9554273666202241, -2.1190393953455047, 0.4580572585843065, -0.6413109094169921, -0.4978755907488374, 0.353842909950826, -1.257672808328245, -0.3236234929243931, -0.8899339209743886, 0.347442395255124, -0.48611580230648704, 1.219007569267497, -1.1789975566918134, -0.01825901882420379, -1.6486718275722763, -0.3819 056170192554, 0.6042625214995715, 0.33415986866928, -0.07450314651627132, 1.1181610738837455, -0.44555621990204264, 2.2166172691235473, -0.6944430028545253, 1.7804248652436612, -0.3801297513557677, 0.1603379135566103, 0.4370378823762644, 0.6537078245920281, -0.5909651262165042, -0.09079544840341715, -0.4588221391142408, 0.3191257940850721, 0.851481629251832, 1.5836138371905788, -0.9214851789009444, -0.40274669796854634, 0.33137905198405093, 0.22048511721877453, -1.6107690385201552, -2.2293481002062734, -1.5862880920203761, -3.021081392986815, 0.4806258491486294, -1.4404361309735374, -0.11292399203207835, -1.3927949945851505, -0.999380367686273, -0.34942295299214127, 0.9614226886733961, -0.011646872308462626, -1.314757063583407, -0.5894619210755432, -0.018747975733725518, 0.7847736709271301, -0.5673683910852766, 0.4350772179247941, -2.310707468789298, -1.3594784507734978, -0.420522194159102, 0.8723173566610757, 0.08704331777378478, 0.19156519877684638, 1.13490819832362, 2.26583242405553 7, 0.08824246678795156, 1.3416771872818614, 2.0113403780125716, -0.6591827547030255, -2.1221005234426045, 0.22388389358930083, -0.35281199160944976, 1.57513125025632, -0.5543596830360817, 0.24276349413614184, -0.6933647399153053, 2.1733561712603406, -1.1295984403806556, 0.978199373546278, 1.4765841816237413, -0.23614652784978474, 0.6860841664188477, 1.724839793220621, 0.09890307698728121, 1.1759901942152857, -0.11367559429189428, 0.03751374982321288, -0.706440448714537, 1.1162223672467126, 0.029899521729224297, -1.0524377764950592, -1.272575346202225, 0.05911556504138499, 2.5567151468399905, -0.0876438255791631, 0.7405000550984376, -0.4653784955900584, 1.0347442227437538, -0.7400874338357808, -1.9149762503590055, -0.23253357169676564, -0.027095878580104853, 1.3433163962194588, 0.6380374697876411, 0.239540766344537, 0.15521652189382637, 1.6840845846495722, -1.2654453459207138, -0.37480522158157953, -0.35072194870832607, -0.13003189765184803, -0.3777510145240076, 1.7368696932480285, 1.59 6933980850992, 0.8903554445814371, 1.5595823556184982, -1.621529094471214, -1.1200493175424486, -0.40963276102036, 0.616137661053976, 0.21520038251543913, -0.10766640285845859, -2.3431682923959283, 0.8376474132653909, -1.624204670319171, -1.5339135491664488, 0.5745962643679627, -1.8672864673583596, 0.5893904049046332, 1.6101909333097806, 1.4942863128478316, 0.4393934760020643, 0.4543320364876582, -2.1477119468636756, -0.8675089134938178, -1.304347506660454, 0.265582855165904, -0.43315684866326853, 1.396958388154523, -0.0366978863358013, 0.7573031920372403, -0.3082978848734662, -1.3293560483520508, -0.014314267765796158, 0.9963287565650347, 0.4441330376836758, -1.1259260419189727, -0.3065655686534148, -1.5814425299338561, -0.6665968885053599, 0.5294914314697337, -0.7787448187072923, -0.5793543872028564, -0.7709908841891148, -0.2752441993676809, 1.0127096639099775, 0.7928535829487908, 0.7274343974269051, -0.3572327111704678, -0.3635339942400348, -1.5135817949705679, -0.8096606039385091, -2.028809935367818, -0.2383975926640008, 0.9888240231357164, -0.46822533717671233, 0.12555572481497712, 0.9249518473038246, -0.7696485018788267, 0.7104271113662229, 0.462549756020218, 0.4661108737616203, -1.3393695430612715, 1.3127387014622585, -0.7204635871833611, -0.15541156481308024, 0.8706609146104812, -2.153190620432126, -1.7876591648613624, -0.08553271346380187, -0.6393327592272366, -1.55129717824011, -0.0068777633683922285, 1.3326516059681166, - 0.36987435658432716, -0.23254064940739874, 0.4093041605249441, -0.22152432261151883, 0.11956045271155907, 0.817312179186691, -1.109714930373252, 1.6919073761823942, -2.477322936459778, -0.8041619336754456, 0.5354776432522204, -1.3994286642549634, 0.7621208634646848, 0.3168358861977863, 0.5654315361895403, 0.19188988525917902, -1.8959135432065441, -0.8883163800042816, -0.07396400493233038, -1.811056975930386, 0.8323666750629728, 0.16495843660377815, 0.9180073204622988, -0.18482724669045245, -0.926597451803997, -0.37373162068547583, 1.2784 981694766253, -0.9622089382124651, 0.36846446000890226, 1.7781522414929132, 0.22598541140652853, 0.0698645511906671, 0.32437679311726797, -1.2473606786833797, -0.4098009718556575, 0.04826245169132581, 0.7412730519089504, -0.2550533401514563, -0.16038376756843503, -1.7248853346384536, -0.9996464058972109, -0.6041936215771648, 0.7012791176135328, 0.2634365350229634, 1.6681642878727803, 0.5006091547003853, 0.8625317708776815, 0.1915138247321982, 0.2611860227007197, 1.1701209557262373, 0.9057134706410637, -1.7953870540423091, 1.2544035731509018, -0.8592198056076387, -0.7480509518320575, -1.4509156168191215, -0.6256261144134664, -0.3991019440510313, 0.665093736894838, 0.8193871205290613, 0.3194454906105449, -0.7766590102568899, -0.4368566309637124, -0.5917671696216537, 0.8311430693910862, -0.03873251937784431, 0.12352685387320661, -1.3032763393594495, -1.3089664655879443, 1.0600394162534208, -1.0448890523458283, 0.06485611850874347, 0.9952192969393494, 0.7564433698789398, -0.129343789502656 82, -2.455965368428643, 0.32562880194284605, 1.0608150376407195, -0.5856546412601992, 0.04459245589226094, -1.0485199770272802, 0.171758532083598, -0.15219755725779346, 0.22588623160615964, -0.8866303888277965, 0.18071263060711168, -0.8101056664306856, 0.40109990179114574, -1.2052661486349423, -0.7063493195672804, 0.3790905532531173, 0.6187801708754183, 0.8973661967856524, -0.5094785611904082, -0.3121531241358417, -1.504997213561154, -0.29555598943521805, 1.0177433955336386, 0.6339433797662462, 1.2314472398672958, 0.49767372261246906, -0.8671596473555494, 0.020938436521665212, 1.0742606122969818, 0.2084859973134108, 0.4575308821288143, -0.535411744646708, -0.24438736749632228, -0.3011498246747746, 1.157476461518601, 0.5710113681137887, -0.373272138340946, 0.20493086120987056, 1.123285194699328, -0.6825221360843674, -0.4970861054585663, 0.7031584488452262, -1.0087051135013718, 0.4868567370256535, -1.5746589820932297, 0.6644556691032865, -1.7412055461143485, 0.12967849664472705, 0.998921 4649538823, -0.8765814845104847, -0.31879367165788586, -0.017003683057253013, 0.05430609428313103, 1.1269963542976742, 0.5736040532976195, 0.1496540697620177, 0.06763527054881197, -1.50267130541346, -1.5429648962225846, 2.2174812208256647, 0.2823275216048955, -2.2106774000464493, -0.750663549795398, -0.9811875317052499, -2.2030641858374813, 0.3394153119839848, 0.8801000780568257, 0.019408804996847228, 0.08061006725110524, -0.7724864191394777, -0.09482684057846079, -0.7973308945882334, -0.004422694306238711, 1.2373993506699386, -0.7465724077874447, 1.0594472039581748, -1.2008095220092798, 0.8786821374927449, 0.5443643385981153, -1.1168872095026448, 0.35057545786785094, 1.177175145035326, 0.8429246083369465, -0.689723978576773, 0.502354026275667, -0.04613819184631698, -0.31654699432372757, 1.5637862738408093, -0.5878311289195507, -1.108863150121421, -0.9675335342352381, -1.5636914224520626, 0.5945700685544679, 0.442348543851493, -0.5701784046610815, -1.0845765562612784, -0.08278380885541 176, -0.8583670878589341, -1.4720479343149773, 2.476311343964019, 0.41098252758504555, -0.5657085013332471, -0.4747296304703354, 1.006084355270273, 0.25723416685361056, -0.6984274306212112, 0.4377896208622477, 0.6460870382990315, 1.3950158568304747, -0.3624333355923981, 0.7986730261282601, 0.36392864932106034, 0.023479591985569596, -2.3188641866554884, 0.5246979591766888, -2.1052930104185146, -0.22436539681910755, -2.3632802785522316, 0.2404756864360818, -0.42347922227477375, -0.649171984257037, -0.2663913129281969, -0.2736238409904541, 0.6799926243706352, -1.8905581628639156, -0.48729302618445974, 0.1364732249240771, -0.654681523885463, -0.09333650364244117, -0.4712888779972051, -0.3481371590459905, -1.0321485704730764, 0.4534634183564675, -0.13910098957798597, -0.43502197202162096, 0.08286272472028665, -1.2802662861780678, 1.0188894812147609, 0.9180346491197053, -0.19780005867554104, -1.7225586291221011, -0.9666520007701466, 0.6167702134637302, -2.0062593053447033, -0.027871345689201 08, -1.3083998368624299, 2.028363730323775, 1.0735441642956547, -1.9121778665959235, -1.2004021402148826, 0.09209714265736849, 0.9735003811225864, 0.5740061725879747, 0.010155866002321106, -0.5414073891346262, -0.3026220001630864, -0.3253578888041597, 1.467401121778751, -0.4019227651608376, 0.9926598684708873, -0.5797202651954227, -0.6032027120399337, -1.5790046534628106, -0.29285533369483696, -0.22908084437071766, -2.7547034239754704, -0.567678718427886, -0.11729223172989703, -0.9062509770861983, 1.1423026021495064, -0.9592198901154894, 0.5837287865704088, 2.3534533038037786, 1.3643136287060391, -0.4191819646187235, -0.5815374749761114, 0.7265232294114061, -0.7689252516062172, -0.6766155124778851, 0.9443044518586721, -0.3076079165374001, -0.39447623686554245, -0.18934519706740402, -1.4654238839941143, 1.3940899668780722, 1.999287460039069, 0.5593145516942836, 0.41146316021623797, 0.7980232784956701, 0.4331041949731728, 0.9824211191778849, -0.49718888243359466, 0.26470386540283247, 0.9 574507682361967, -0.874516288215613, -1.9849619724051226, -0.558877120925908, -0.5071314855164495, -0.7956992895178723, -1.2616310109098234, -0.6810345434093048, -1.3306339462185115, -0.6894885013031004, -0.7110844934740447, 1.0976801504693237, -0.282672024430709, -0.3552224671688322, 0.3481565774418335, -2.2169474805300355, -2.2354740407597724, 1.6562225265616324, 0.03230440501913142, 0.5592484434095385, -1.0240995675521865, 1.921011636618146, -1.8425494238779783, 0.5371478992314899, 0.1177459062388671, -0.1696263342284998, 0.35215096349316727, -0.024862173384408784, -0

You need to reinitialize your arrList1 at every iteration of the outer loop:

for(int j = 0; j < strArr.length; j++){
    arrList1 = new ArrayList<Double>();
    for(int i = 0; i < 50; i++){
       arrList1.add(rand.nextGaussian());
    }
    arrList2.add(arrList1);
}

otherwise you keep adding to the same ArrayList , which is the reason for the huge output you are getting.

You're using the same instance for arrList1 throughout.

Try:

for(int j=0 ; j< strArr.length; j++){
    arrList1 = new ArrayList<Double>();
    for(int i =0 ;i < 50 ; i++){
        arrList1.add(rand.nextGaussian());
    }
    arrList2.add(arrList1);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM