嵌套用于循环存储到数组中,只将一列嵌套循环放入数组中

| 因此,我要做的是遍历一张1024x768的图片(或popMap.getWidth x popMap.getHeight),抓取它的蓝色,将其与到目前为止的最高蓝色进行比较,如果更重要的是,蓝色变成了新的\'newBlue \'。基本上,找到图像中最高的蓝色值,最接近255蓝色。 另外,我正在尝试将整个对象的蓝色值存储到popMapArray数组中,该数组是一个具有3列的2d数组,用于存储[blueValue] [x] [y]。然后我将对其进行排序,以获得从最蓝值的高到低的列表。 我的问题是,使用下面的代码,当column = 767时,它仅存储到数组中。 我得到1024 [blue,row,767],然后其余全部为[0,0,0] 有什么线索吗?顺便说一下,Java。
for (int row = 0;  row < popMap.getWidth(); row++) 
    {
        for (int column = 0; column < popMap.getHeight(); column++)
        {
            System.out.println(column);
            //Find a Pixel
            int c = popMap.getRGB(row, column);
            int red = (c & 0x00ff0000) >> 16;
            //int  green = (c & 0x0000ff00) >> 8;
            //int  blue = c & 0x000000ff;
            // and the Java Color is ...
            Color color = new Color(red); 
            int newBlue = color.getBlue();
            int oldBlue = lastColor.getBlue();
            switch(popArrayRow)
            {
                case 0:
                {
                    arrayVar = newBlue;
                    popArrayRow = 1;
                    break;
                }
                case 1:
                {
                    arrayVar = row;
                    popArrayRow = 2;
                    break;
                }
                case 2:
                {
                    arrayVar = column;
                    popArrayRow = 0;
                    break;
                }
            }
            popArray[row][popArrayColumn] = arrayVar;
            //System.out.println(popArray[row][popArrayColumn]);
            switch(popArrayColumn)
            {
                case 0:
                {
                    popArrayColumn = 1;
                    break;
                }
                case 1:
                {
                    popArrayColumn = 2;
                    break;
                }
                case 2:
                {
                    popArrayColumn = 0;
                    break;
                }
            }


            if(newBlue > oldBlue)
            {
                startX = row;
                startY = column;
                //System.out.print(row);
                //System.out.print(\",\");
                //System.out.println(column);
                System.out.print(\"The oldBlue is \");
                System.out.println(oldBlue);
                lastColor = color;
            }



        }
    } 
    
已邀请:
您没有显示声明转到ѭ1(以及其他一些我认为是初始化为0的int变量)。您将其描述为“具有3列的2d数组”。我猜您已将其声明为
int[1024][3]
,因此在
popMap
中每行有一行,然后是您的3个“ columns”,用于存储蓝色值,原始x坐标和原始y坐标。 因此,首先不清楚您希望如何在阵列中为原始图像中的每个像素存储一个条目。但是,也许我对您如何声明它是错误的猜测。 无论如何,您每次都要通过内循环进行设置
popArray[currentPixel] = {blueValue, origX, origY}
但是相反,您每次循环都只分配三个值之一。所以你正在做类似的事情
popArray[0][0] = blueValue //first iteration; blueValue from row 0 col 0
popArray[0][1] = 0 //second iteration; row from row 0 col 1
popArray[0][2] = 2 //third iteration; column from row 0 col 2
因此,希望您已经看到有问题,因为您正在填充应该与来自循环的不同迭代的值一起使用的“列”。更糟糕的是,您随后在内部循环的下一次迭代中开始覆盖这些值(它将在
row
递增之前总共进行768次迭代):
popArray[0][0] = blueValue // fourth iteration; blueValue from row 0 col 4; overwrite value assigned on first iteration
etc...
与其使用具有不同含义的3个数组“ columns”来保存这些数据元素,不如创建一个包含三个值并弄清楚什么是什么的类。
popArray
将包含此对象类型。此外,我将它设为
List
而不是数组,因为它更加灵活,您可以在末尾简单地调用
Collections.sort()
。或@jsegal建议使用一种数据结构,该数据结构会在插入项目时进行排序。哪个更好,可能取决于您以后要对他们做什么。     
int red = (c & 0x00ff0000) >> 16;
        //int  green = (c & 0x0000ff00) >> 8;
        //int  blue = c & 0x000000ff;
        // and the Java Color is ...
        Color color = new Color(red); 
       int newBlue = color.getBlue();
您是说“颜色=新颜色(c)”吗?您的newBlue值将始终为0 ... 另外,您到底想使用popArray构造什么?让状态变量每个像素调整一次可能无法完成您想要的...听起来像您想要一个
SortedMap<int,Point>
,键入blueValue,其值是点的x,y坐标(存储为点数组或Point对象)。然后,您将具有按蓝色值排序的数据结构,并且可以直接读取要点。 祝你好运!     

要回复问题请先登录注册