java的:如何将二维数组分成两个二维数组 [英] java: How to split a 2d array into two 2d arrays

查看:193
本文介绍了java的:如何将二维数组分成两个二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个程序,以尽可能有效地乘矩阵(二维数组),为此我需要我的两个数组分成两个各发送了他们对要乘以第二个程序。我的问题是如何将一个二维数组分成两个二维数组,在特定的点(中间)。没有人有任何想法?

I'm writing a program to multiply matrices (2d arrays) as efficiently as possible, and for this i need to split my two arrays into two each and send them off to a second program to be multiplied. The issue I have is how to split a 2d array into two 2d arrays, at specific points (halfway). Does anyone have any ideas?

推荐答案

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

Lets say you have a 2d array of strings like so



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

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

Now you need a way to split these arrays at the half way point. Lets get the halfway point. Figure out how big the array is and then cut it in half. Note that you also must handle if the array is not an even length. Example, length of 3. If this is the case, we will use the Math.floor() function.



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;

现在,我们有我们需要从一个创建两个二维数组中的所有信息。现在,我们必须明确地复制创建并在数据复制。

Now we have all the info we need to create two 2d arrays from one. Now we must explicitly copy create and copy the data over.



//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];
}

和你做!

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

Now if you want to do it a little nicer, you could do it like this



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 <一连结href=\"http://download.oracle.com/javase/1.4.2/docs/api/java/lang/System.html#arraycopy%28java.lang.Object,%20int,%20java.lang.Object,%20int,%20int%29\"相对=nofollow> System.arraycopy()

And lastly, if you dont want to do it explicitly, you can use the built in functions. System.arraycopy() is one example. Here is a link to its 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);

这篇关于java的:如何将二维数组分成两个二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆