Java - Adding Rows is there a more efficient way? -
below code, wondering if others coding differently. maybe can make small changes. in advance!
public class addrow { public static int[][] insert(int [][] a, int [] row, int index){ int [][] x = new int[a.length+1][a.length]; int [] temp; for(int i=0; i<x.length-1; i++) { x[i] = a[i]; if(i == index) { temp = a[i]; x[i] = row; x[i+1] = temp; } } return x; } public static void main(string[] args){ int[][] = {{1,2,3},{4,5,6},{10,11,12}}; int[] row = {7,8,9}; int [][] b = insert(a,row ,2); for(int r=0; r < b.length; r++){ for(int c=0;c< b[r].length; c++){ system.out.print(b[r][c] + " "); }system.out.println(); } } }
system.arraycopy
answer.
it uses native code copy arrays in memory directly, making more efficient.
Comments
Post a Comment