打破大型二维数组成多个更小的二维数组使用JAVA [英] Break large 2D array into multiple smaller 2D array using JAVA

查看:153
本文介绍了打破大型二维数组成多个更小的二维数组使用JAVA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二维数组其由其中其大小取决于输入图像的尺寸的图像像素。我需要把它分解成更小的9x9的阵列。为了给出一个更清晰的画面,我尽量说明情况:

I have a 2D array which consist of image pixels which its size depends on the size of the input image. I need to break it into smaller 9x9 arrays. To give a clearer picture, I try to illustrate the situation:

//将smallerArray的行数和列数会是这个样子:
它应该从imagePixels阵列通过每个8列迭代复制它们,然后移动到下一个8行。

//The number of rows and columns of the smallerArray will look something like this: It should copy them from the imagePixels array iterating through each 8 columns, then move on to the next 8 rows.

    1st small   2nd small            3rd small            and so on..
    array:      array:               array: 
     012345678  91011121314151617   181920212223242526    ....
    0          0                   0
    1          1                   1
    2          2                   2
    3          3                   3
    4          4                   4
    5          5                   5
    6          6                   6
    7          7                   7
    8          8                   8

    Then move on to next 8 rows:

      012345678    91011121314151617   181920212223242526    ....
    9             9                   9
    10           10                  10
    11           11                  11
    12           12                  12
    13           13                  13
    14           14                  14
    15           15                  15
    16           16                  16
    17           17                  17

.
.
.

我做了以下code,但不能让我的逻辑正确的。我怎样才能停止迭代:

I have done the following code but could not get my logic right. How can I stop the iteration:

复制,直到第9列或行,将其存储在一个数组,继续复制在第10行/列,停止时后到达9列/行,将其存储在另一个数组,并继续这样做,直到它完复制imagePixels阵列。

Copy up until the 9th column or row, store it in an array, resume copying on the 10th column/row, stop when it reaches 9 column/row after, store it in another array, and keep on doing that till it finishes copying the imagePixels array.

我试图将数组存储在一个ArrayList所以这将是我更容易操作和计算做的东西与所有的smallerArrays。

I tried to store the arrays in an ArrayList so it would be easier for me to manipulate and do calculation stuffs with all the smallerArrays.

ArrayList<double[][]> collectionofSmallArrays = new ArrayList();
double[][] imagePixels = new double[1600][1000]; //Lets say this is the size of the image
double[][] smallerArray = new double[9][9];

for(int a = 0; a<smallerArray.length; a++)
{
  for(int b =0; b<smallerArray[a].length; b++)
  { 
    for(int c=0; c<imagePixels.length; c++)
    {
      for(int d=0; d<imagePixels[c].length; d++)
      {
        smallerArray[a][b] = imagePixels[c][d];
        ...(code to stop the iteration if it reaches 9, store it in array then resume where it stops     with another new array)
        collectionofSmallArrays.add(smallerArray);
      }
    }
  }
}

任何人都可以解决了code,达到了预期效果?鸭preciate它。

Can anyone work around the code to achieve the expected result? Appreciate it.

推荐答案

您或许应该提供更多的上下文信息。话说的数组双值重新presents的像素的声音dubios。如果您在使用图像,那么你可能会找到解决方案的完全的不同层次的抽象(我想<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html#getSubimage%28int,%20int,%20int,%20int%29\"相对=nofollow>的BufferedImage#createSubImage 这里)。

You should probably provide more context information. Saying that an array of double values represents pixels sounds dubios. If you are working with images, then you might find solutions on a completely different level of abstraction (I'm thinking about BufferedImage#createSubImage here).

不过,要回答这个问题:你应该把任务分成更小的部分。尤其,这可能是更容易实现两种方法:

However, to answer the question: You should break the task up into smaller parts. Particularly, it might be easier to implement two methods:


  • ,其接收一输入阵列,一些坐标,和一个输出阵列,并且其中一个方法是将文件从输入阵列到输出数组指定坐标的数据

  • 调用与适当坐标上述方法对整个阵列分割成期望的大小的部件的一种方法。

在伪code:

for (each coordinates (9*r,9*c)) {
    copy the rectange (9*r,9*c)-(9*r+9,9*c+9) of the input array into an array

一个非常简单的实现/测试示于下面的例子。请注意,这可能是推广和改进,但我认为这显示了基本思路:

A very simple implementation/test is shown in the following example. Note that this could be generalized and improved, but I think it shows the basic idea:

import java.util.ArrayList;
import java.util.List;

public class SubArrayTest
{
    public static void main(String[] args)
    {
        double inputArray[][] = createInputArray(160, 100);
        System.out.println("inputArray: "+toString(inputArray));

        List<double[][]> subArrays = createSubArrays(inputArray, 9, 9);

        for (double subArray[][] : subArrays)
        {
            System.out.println("subArray:\n"+toString(subArray));
        }
    }

    private static List<double[][]> createSubArrays(
        double inputArray[][], int subRows, int subCols)
    {
        List<double[][]> subArrays = new ArrayList<double[][]>();
        for (int r=0; r<inputArray.length; r+=subRows)
        {
            for (int c=0; c<inputArray[r].length; c+=subCols)
            {
                double subArray[][] = new double[subRows][subCols]; 
                fillSubArray(inputArray, r, c, subArray);
                subArrays.add(subArray);
            }
        }
        return subArrays;
    }

    private static void fillSubArray(
        double[][] inputArray, int r0, int c0, double subArray[][])
    {
        for (int r=0; r<subArray.length; r++)
        {
            for (int c=0; c<subArray[r].length; c++)
            {
                int ir = r0 + r;
                int ic = c0 + c;
                if (ir < inputArray.length &&
                    ic < inputArray[ir].length)
                {
                    subArray[r][c] = inputArray[ir][ic];
                }
            }
        }
    }


    //===Test methods=========================================================
    private static double[][] createInputArray(int rows, int cols)
    {
        double array[][] = new double[rows][cols]; 
        for (int r=0; r<array.length; r++)
        {
            for (int c=0; c<array[r].length; c++)
            {
                array[r][c] = r*100+c;
            }
        }
        return array;
    }

    private static String toString(double array[][])
    {
        String format = "%7.1f";
        StringBuilder sb = new StringBuilder();
        for (int r=0; r<array.length; r++)
        {
            for (int c=0; c<array[r].length; c++)
            {
                sb.append(String.format(format, array[r][c])+" ");
            }
            sb.append("\n");
        }
        return sb.toString();
    }


}


旁注:


  • 您应该总是与他们的接口类型声明变量。这意味着,你不应该写

  • You should always declare variables with their interface type. This means that you should not write

ArrayList<String> x = new ArrayList<String>();

但总是

List<String> x = new ArrayList<String>();


  • 在公布code,你似乎总是添加同一个实例子数组列表即可。这意味着,在最后你会在列表中的同一阵列的多个实例,都具有相同的内容。介于两者之间,你必须创建一个新的双[9] [10] 阵列。

  • In the posted code, you seemed to always add the same instance of the sub-array to the list. This means that in the end you would have many instances of the same array in the list, all with the same content. Somewhere in between you have to create a new double[9][9] array.

    这篇关于打破大型二维数组成多个更小的二维数组使用JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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