Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的代码不';t返回我的arraylist的大小?_Java_Android_Arraylist_Opencv4android - Fatal编程技术网

Java 为什么我的代码不';t返回我的arraylist的大小?

Java 为什么我的代码不';t返回我的arraylist的大小?,java,android,arraylist,opencv4android,Java,Android,Arraylist,Opencv4android,我的代码读取图像并将像素值保存在double[]类型的数组中。但是,我需要一个动态大小,所以我创建了一个arraylist,并将数组添加到arraylist中。问题是我无法获取数组的大小,也无法从arraylist获取任何信息。它根本没有告诉我任何东西。有什么问题吗?请参阅下面的代码 Java for android public class img_pixel { String src = "path_of_image.jpg"; Mat imgRead = Imgcodec

我的代码读取图像并将像素值保存在double[]类型的数组中。但是,我需要一个动态大小,所以我创建了一个arraylist,并将数组添加到arraylist中。问题是我无法获取数组的大小,也无法从arraylist获取任何信息。它根本没有告诉我任何东西。有什么问题吗?请参阅下面的代码

Java for android

public class img_pixel {
    String src = "path_of_image.jpg";
    Mat imgRead = Imgcodecs.imread(src, IMREAD_COLOR); //read a image
    int row = imgRead.rows(); //get the number of rows
    int col = imgRead.cols(); //get the number of cols

    List <double[]> pixels = new ArrayList<>(); //Arraylist 

    public void cor() {
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                double[] rgb = imgRead.get(i, j); //array with information i, j
                pixels.add(0, rgb);
            }
        }
        for (int x = 0; x < pixels.size(); x++) { // for to get size of arraylist
            Log.v("Size", "Size:" + pixels.get(x));
        }
    }
}
Java for android
公共类img_像素{
字符串src=“path\u of_image.jpg”;
Mat imgRead=Imgcodecs.imread(src,imread_COLOR);//读取图像
int row=imgRead.rows();//获取行数
int col=imgRead.cols();//获取col的数量
列表像素=新建ArrayList();//ArrayList
公共图书馆{
对于(int i=0;i
您的问题表明您对arraylist或arrays的工作方式缺乏了解

  • 您的代码已经“获取”了arraylist大小。在第二个for循环中,调用
    pixels.size()
    。该方法返回arraylist的长度。因此,您对“也不能从arraylist获取任何信息”的理解是错误的
  • 似乎您想知道的是每个像素的阵列大小。我不知道为什么,因为对于一张给定的图片,每一张都是一样的,但让我们过去看看。在您的代码中,您有
    Log.v(“大小”,“大小:”+pixels.get(x))我假定这是您尝试获取数组大小的结果。但是,pixels.get(x)仅返回数组对象,因此您似乎正在尝试将类型为
    double[]
    的内容转换为字符串,并希望看到数组大小。你看到问题了吗?如果您想要数组的“大小”(我假设您是指“长度”),则需要使用length属性,例如
    pixels.get(x).length
    。如果你真的想要这个尺寸,你需要做更多的工作。我建议先弄清楚您真正需要什么信息,然后再做一些研究,比如“如何在Java中获得数组的大小”

  • 如果你想知道双数组在arraylist位置x的大小,只需调用

    pixels.get(x).length;
    

    但很抱歉,我不太清楚这个问题

    for(int i=0;i
    其中是
    lin
    pixels.get(x)
    返回一个
    double[]
    。将
    double[]
    转换为
    字符串会产生类似
    ”的结果[I@23fc4bec"
    。什么是变量lin?lin变量和col被row和col替换,因为它们已经在这里复制了代码,所以我没有意识到我很抱歉。我编辑了。@Turing85如何返回整数?在arraylist中有像素值…我想查看这个值。还有查看数据(像素)的提示吗这是arraylist“pixels”中的内容?@BDamsk我建议找到一些教程,这些教程包含示例代码,涵盖您正在尝试执行的操作。我已解决。谢谢。日志不会出现,因为我没有在主类中对img_pixel类进行初始化。