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.

http://docs.oracle.com/javase/6/docs/api/java/lang/system.html#arraycopy%28java.lang.object,%20int,%20java.lang.object,%20int,%20int%29

it uses native code copy arrays in memory directly, making more efficient.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -