java:如何将2d数组拆分为两个2d数组

我正在编写一个尽可能高效地乘法矩阵(2d数组)的程序,为此我需要将我的两个数组分成两个,然后将它们发送到第二个程序进行乘法运算。 我遇到的问题是如何在特定点(中途)将2darrays分成两个2darrays。 有没有人有任何想法?

假设你有一个像这样的二维数组字符串

String[][] array= new String[][] { {"a","b","c"}, {"d","e","f"}, {"h","i","j"}, {"k","l","m"} }; 

现在,您需要一种方法在中间点分割这些数组。 让我们得到中途点。 弄清楚arrays有多大,然后把它切成两半。 请注意,如果数组不是偶数长度,您还必须处理。 例如,长度为3.如果是这种情况,我们将使用Math.floor()函数。

int arrayLength = array.length; int halfWayPoint = Math.floor(arrayLength/2); //we also need to know howmany elements are in the array int numberOfElementsInArray = array[0].length;
int arrayLength = array.length; int halfWayPoint = Math.floor(arrayLength/2); //we also need to know howmany elements are in the array int numberOfElementsInArray = array[0].length; 

现在我们拥有了从一个创建两个2d数组所需的所有信息。 现在我们必须明确地复制创建和复制数据。

//the length of the first array will be the half way point which we already have String [][] newArrayA = new String[halfWayPoint][numberOfElementsInArray]; //this copies the data over for(int i = 0; i < halfWayPoint; i++) { newArrayA[i] = array[i]; } //now create the other array int newArrayBLength = array.length - halfWayPoint; String[][] newArrayB = new String[newArrayBLength][numberOfElementsInArray]; /* * This copies the data over. Notice that the for loop starts a halfWayPoint. * This is because this is where we left of copying in the first array. */ for(int i = halfWayPoint; i < array.length; i++) { newArrayB[i] = array[i]; }
//the length of the first array will be the half way point which we already have String [][] newArrayA = new String[halfWayPoint][numberOfElementsInArray]; //this copies the data over for(int i = 0; i < halfWayPoint; i++) { newArrayA[i] = array[i]; } //now create the other array int newArrayBLength = array.length - halfWayPoint; String[][] newArrayB = new String[newArrayBLength][numberOfElementsInArray]; /* * This copies the data over. Notice that the for loop starts a halfWayPoint. * This is because this is where we left of copying in the first array. */ for(int i = halfWayPoint; i < array.length; i++) { newArrayB[i] = array[i]; } 

而你完成了!

现在,如果你想做得更好一点,你可以这样做

int half = Math.floor(array/2); int numberOfElementsInArray = array[0].length; String [][] A = new String[half][numberOfElementsInArray]; String [][] B = new String[array.length - half][numberOfElementsInArray]; for(int i = 0; i < array.length; i++) { if(i < half) { A[i] = array[i]; } else { B[i] = array[i]; } }
int half = Math.floor(array/2); int numberOfElementsInArray = array[0].length; String [][] A = new String[half][numberOfElementsInArray]; String [][] B = new String[array.length - half][numberOfElementsInArray]; for(int i = 0; i < array.length; i++) { if(i < half) { A[i] = array[i]; } else { B[i] = array[i]; } } 

最后,如果您不想明确地执行此操作,则可以使用内置函数。 System.arraycopy()就是一个例子。 这是api System.arraycopy()的链接

int half = Math.floor(array/2); int numberOfElementsInArray = array[0].length; String [][] A = new String[half][numberOfElementsInArray]; String [][] B = new String[array.length - half][numberOfElementsInArray]; System.arraycopy(array,0,A,0,half); System.arraycopy(array,half,B,0,array.length - half);
int half = Math.floor(array/2); int numberOfElementsInArray = array[0].length; String [][] A = new String[half][numberOfElementsInArray]; String [][] B = new String[array.length - half][numberOfElementsInArray]; System.arraycopy(array,0,A,0,half); System.arraycopy(array,half,B,0,array.length - half);