简体   繁体   English

将二维数组与一维数组组合

[英]Combining 2D array with a 1D array

I have 2 arrays one is a int[][] pixelYArray and the other one is a int[] pixelXArray .我有 2 arrays 一个是int[][] pixelYArray另一个是int[] pixelXArray I can not set the full width and height of the image as lenghts because these pixels are already filtered on.我无法将图像的整个宽度和高度设置为长度,因为这些像素已经被过滤掉了。 For example having the color yellow and these are the left over pixels and I want to create an array or object like this:例如,颜色为黄色,这些是剩余的像素,我想创建一个数组或 object,如下所示:

 [
  {
    "pixel x" : 1,
    "pixel y" : [
      1, 2, 3, 100, 101, 102
    ]
  },{
    "pixel x" : 3,
    "pixel y" : [
      1, 2, 32, 100, 101, 102
    ]
  },
]

the text "pixel x" and "pixel y" are optional and not needed if its makes it easier to create the array.文本"pixel x""pixel y"是可选的,如果它使创建数组更容易则不需要。 In Javascript you can achieve it by doing something like this在 Javascript 你可以通过做这样的事情来实现它

let array = [];
for(int i=0;i < pixelYArray.length;i++){
    let tmpArr = [];
    for(int j=0;j < pixelYArray[i].length; j++){
        tmpArr.push(pixelYArray[i][j]);
    }
    array.push({
        "pixel x" : pixelXArray[i],
        "pixel y" : tmpArr
    });
    /* or
    array.push({
        pixelXArray[i],
        tmpArr
    }); */
}

only how can you achieve this in Java because you can not create objects like this or push them into an array.只是你怎么能在 Java 中实现这个,因为你不能像这样创建对象或将它们推入数组。 I have a for loop like this:我有一个这样的 for 循环:

   for(int i=0;i < pixelYArray.length;i++){
        System.out.println("pixel x: " + pixelXArray[i] + " pixel y: ");
        for(int j=0;j < pixelYArray[i].length; j++){
            System.out.print(pixelYArray[i][j] + "\t" );
        }
        System.out.println("\nEnd of Pixels for row \n");
    }

what gives an result like this and many more layers of X pixels and y pixels ofc是什么给出了这样的结果以及更多的 X 像素和 y 像素层作为循环结果的 X 和 Y 像素行

I think this will solve your problem:我认为这将解决您的问题:

public class Test {
    public static void main(String[] args) {
        List<Color> list = new ArrayList<>();
        Color c = new Color();
        c.pixel_x = 1;
        c.pixel_y.add(1);
        c.pixel_y.add(2);
        c.pixel_y.add(3);
        c.pixel_y.add(100);
        c.pixel_y.add(101);
        c.pixel_y.add(102);

        list.add(c);

        System.out.println(c);
    }
}
class Color {
    public int pixel_x;
    public List<Integer> pixel_y;

    public Color() {
        pixel_x = 0;
        pixel_y = new ArrayList<>();
    }

    @Override
    public String toString() {
        return pixel_x + " " + pixel_y;
    }
}

First you create a Color object that contains pixel_x of type Integer and pixel_y of type List<Integer> then in your main, every time you create a Color instance, add it to List<Color> .首先创建一个Color object,其中包含类型为pixel_xInteger和类型为List<Integer>pixel_y ,然后在您的 main 中,每次创建Color实例时,将其添加到List<Color>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM