java - Can a 2D array hold an array within a given point? -


i newbie programmer question on arrays in java. consider 2d array, [i][j]. value of determined @ run time. value of j known 7. @ [i][6] , [i][7] want able store deeper array or list of values. possible have array within array, there x , y axis , z axis @ point of [i][6] , i[7] or need full 3d cube of memory able store , navigate data?

the details: goal run query takes information 2 tables (target , attacker) query fine , can resultset. want able store data resultset , present in table in more useful format while using in data visualization program. fields are: server_id, target_ip, threat_level, client_id, attacker_ip , num_of_attacks. 20 records have same server_id, target_ip, threat_level, client_id different attacker_ip , num_of_attacks because machine got attacked 20 times. third dimension allow me 3rd axis/array empty server_id, target_ip, threat_level, client_id

update after reviewing answers , doing more thinking i'm wondering if using arraylist of objects best me, and/or possible. keeping data organized , accessible big concern me. in psedu code this:

object[] servers     string server_id     string target     string threat_level     string client_id     string arr[][]   // array hold attacker_ip in 1 axis , num_of_attacks in other in order keep relation between attacking ip , number of attacks make against 1 specific server 

in first place, if have array datatype[i][j] , j known 7, 2 greatest indexes can use 5 , 6, not 6 , 7. because java array indexes 0-based. when creating array indicate number of elements, not maximum index (which 1 less number of elements).

in second place, there nothing wrong using multidimensional arrays when problem domain uses them. can think of scientific applications, data analysis applications, not many more. if, on contrary, modelling business problem domain not use multidimensional arrays, better off using more abstract data structures instead of forcing arrays design because seem efficient, experience in other languages arrays more important, or other reasons.

without having information, i'd "first dimension" better represented list type (say arraylist). why? because size determined @ runtime (and assume comes indirectly, not magic number obtain somewhere). lists similar arrays have particularity "know" how grow. program can append new elements reads them source or otherwise discovers/creates them. can insert them @ beginning or in middle, rare.

so, first dimension be: arraylist<something>, something type of second dimension.

regarding second dimension, has size of 7, first 5 items accept single values while last 2 multiple ones. telling me 7 items not homogeneous, , array ill-indicated. dimension better represented class. understand class's structure, let's 5 single-valued elements homogenous (of type, say, bigdecimal). 1 of natural representations array, size known. 2 remaining, multi-valued elements seem constitute array. however, given each of 2 elements contains unidentified number of data items, element type of array should not bigdecimal in previous case, arraylist. type of elements of these arraylists whatever type of multiple values (say bigdecimal too).

the final result is:

class secondd {     bigdecimal[] singlevalued= new bigdecimal[5] ;     arraylist<bigdecimal>[] multivalued= new arraylist<bigdecimal>[2] ;     {         multivalued[0]= new arraylist<bigdecimal>() ;         multivalued[1]= new arraylist<bigdecimal>() ;     } } arraylist<secondd> data= new arraylist<secondd>() ; 

in code snippet i'm not declaring structures, creating them ready use. pure declaration be:

class secondd {     bigdecimal[] singlevalued;     arraylist<bigdecimal>[] multivalued; } arraylist<secondd> data= new arraylist<secondd>() ; 

array size not important in java type (and structural) point of view. that's why don't see 5 or 2.

access data structure like

data.get(130).singlevalued[2] data.get(130).multivalued[1].get(27) 

a possible variant clearer in cases

class secondd {     bigdecimal monday;     bigdecimal tuesday;     bigdecimal wednesday;     bigdecimal thursday;     bigdecimal friday;     arraylist<bigdecimal> saturday= new arraylist<bigdecimal>() ;     arraylist<bigdecimal> sunday= new arraylist<bigdecimal>() ; } arraylist<secondd> data= new arraylist<secondd>() ; 

in case "expanding" each array individual items, each name. typical access operations be:

data.get(130).wednesday data.get(130).sunday.get(27) 

which variant choose? well, depends on how similar or different operations different itemes are. if every time perform , operation monday perform tuesday, wednesday, thursday, , friday (not saturday , sunday because these different kind of thing, remember?), array better. example, sum items when stores array it's necessary:

element= data.get(130) ; int sum= 0 ; for(int e: element.singlevalued ) sum+= e ; 

while if expanded:

element= data.get(130) ; int sum= 0 ; sum+= element.monday ; sum+= element.tuesday ; sum+= element.wednesday ; sum+= element.thursday ; sum+= element.friday ; 

in case, 5 elements, difference not much. first way makes things shorter, while second makes them clearer. personally, vote clarity. now, if instead of 5 items have been 1,000 or few 20, repetition in second case have , first case preferred. have general rule too: if can name every element separately, it's better so. if while trying name elements find myself using numbers or sequential letters of alphabet (either naturally, in days of month, or because things don't seem have different names), it's arrays. still find cases not clear after applying these 2 criteria. in case toss coin, start developing program, , think bit how things other way. can change mind time.

if application indeed scientific one, please forgive me such long (and useless) explanation. answer others looking similar, though.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -