Monday 15 July 2013

java - Multidimensional array column swap -



java - Multidimensional array column swap -

after identifying elements in specific column of multidimensional array not numbers i'm looking @ swapping 2 columns of multidimensional array.

basically swap contents of column 4 in column 6 after determining whether elements in column 6 numbers or not if elements in column 6 not numbers create new array , start inserting old array contents in new array 1 time column gets 4th column of old array places element in 6th column of new array , vice versa.

below effort regarding above. i've been able identify if elements in column 6 numbers or not. have doubts regarding logic behind swapping elements in column 4 in 6.

//!< public string[][] sortedbydateusagestatsdata(string[][] unsortedusagestatsdata) { system.out.println("\nbtnusagestats - sortedbydateusagestatsdata(string[][] unsortedusagestatsdata)"); (int row = 0; row < unsortedusagestatsdata.length; row++) { (int col = 0; col < 7; col++) { //system.out.println("unsortedusagestatsdata[" + row + "][" + col + "] => " + unsortedusagestatsdata[row][col]); } // check if content in homecoming code column i.e. column 6 number or string. // if string swap column 4 content column 6 , pass array swapped columns final array if(!isnumericstring(unsortedusagestatsdata[0][6])) { //system.out.println("unsortedusagestatsdata[0][6] not number i.e. " + unsortedusagestatsdata[0][6]); break; } system.out.println("\n"); } system.out.println("\nexited loop unsortedusagestatsdata[0][6] not number\n"); sortedusagestatsdata = new string[unsortedusagestatsdata.length][7]; (int row = 0; row < unsortedusagestatsdata.length; row++) { (int col = 0; col < 7; col++) { //system.out.println("\nbefore swap unsortedusagestatsdata[" + row + "][" + col + "] => " + unsortedusagestatsdata[row][col]); sortedusagestatsdata[row][col] = unsortedusagestatsdata[row][col]; if(col == 4) { system.out.println("\nbefore swap sortedusagestatsdata[" + row + "][6] => " + unsortedusagestatsdata[row][6]); sortedusagestatsdata[row][6] = unsortedusagestatsdata[row][4]; system.out.println("\nafter swap sortedusagestatsdata[" + row + "][6] => " + unsortedusagestatsdata[row][6]); sortedusagestatsdata[row][4] = unsortedusagestatsdata[row][6]; system.out.println("\nafter swap sortedusagestatsdata[" + row + "][4] => " + unsortedusagestatsdata[row][4]); } system.out.println("\nafter swap sortedusagestatsdata[" + row + "][" + col + "] => " + unsortedusagestatsdata[row][col]); } system.out.println("\n"); } homecoming sortedusagestatsdata; }

any help appreciated.

there's problem loop. in iteration col == 6 override value of sortedusagestatsdata[row][6] had set before (when col 4).

there few ways alter this, i'll name two:

iterate 0 5 only: for (int col = 0; col < 6; col++).

set values of columns 4 , 6 in corresponding iterations:

if(col == 4) { sortedusagestatsdata[row][4] = unsortedusagestatsdata[row][6]; } else if (col == 6) { sortedusagestatsdata[row][6] = unsortedusagestatsdata[row][4]; }

java multidimensional-array

No comments:

Post a Comment