java - Staggered two dimensional arrays? -
so have bunch of id numbers align sort of tree.... example
1 3082 2986 / \ / \ 2 1233 2344 1233 1634 / \ / \ / \ 3 0254 0456 1342 0653 0643 0463
how make array of array fit scheme or there else do? know make 2-dimensional array (string[][] array = new ...) size each tree different , unknown. 1x1 , others 3x5, @ largest. don't want have make array large either, @ least don't think do.
i using java , starts in jsonarray way.
update: have added examples of id numbers be, kind of irrelevant. if combined 0643 , 0463 1634, if combined 1643 1233, final item 2986. again irrelevant how works.
i want know if possible make 2-dimensional array if size of array unknown, can see column 1 contain 1 id number, column 2 contain 2 id numbers , in cases 1 or 3 id numbers, , column 3 can contain anywhere 1 6 id numbers.
i made demo-program see how 1 implement 2-d array unknown set of columns , rows -- use arraylist class first store unknown set of items, add 2-d array. take note of syntax use in creating ragged 2-d array: (note; ragged 2-d array array different number of columns in each row)
import javax.swing.joptionpane; import java.util.arraylist; public class ss { public static void main(string[] args){ joptionpane.showmessagedialog(null, "welcome woodrow's program.\n" + "here, create 2d array big want.. 2d array demo."); arraylist<string> names = new arraylist<>(); while(true) { string nameinput = joptionpane.showinputdialog("enter names here: hit cancel when done."); if(nameinput == null || nameinput.equals("")) break; names.add(nameinput); } string[][] pplinfo = new string[names.size()][]; for(int = 0, accumulator = 0; < names.size(); i++) { arraylist<string> pets = new arraylist<>(); int inneraccumulator = -1; string petinput = ""; do{ petinput = joptionpane.showinputdialog("enter names of " + names.get(accumulator) + "'s pets here: (hit cancel when done)"); inneraccumulator++; if(petinput != null || petinput.equals("")) { pets.add(petinput); } }while(petinput != null && !petinput.equals("")); accumulator++; pplinfo[i] = new string[inneraccumulator]; //the row of person i, going have inneraccumulator columns listing pets. //now add pets appropriate row for(int ii = 0; ii < inneraccumulator; ii++) pplinfo[i][ii] = pets.get(ii); } //now display data have entered in ragged array. //ragged means number of columns differs each row. for(int = 0; < pplinfo.length; i++) {//pplinfo.length returns number of rows system.out.println(names.get(i) + "'s pet list:"); for(int ii = 0; ii < pplinfo[i].length; ii++) //pplinfo[rowindex].length returns # columns in respective row system.out.println((ii+1) + ". " + pplinfo[i][ii]); system.out.println("======================"); } system.exit(0); } }
i start with:
string[][] str = new string[rows][];
the first box specify int, number of rows in array. leave second bracket blank now; later, specify number of columns in each row doing:
str[index] = new string[columns];
now, have 2-d array size want, , can add items in nested loop.
Comments
Post a Comment