简体   繁体   English

如何在java中声明通用ArrayLists(2d Arraylists)的ArrayList?

[英]How to declare an ArrayList of Genric ArrayLists (2d Arraylists) in java?

So I've been trying to create an ArrayList of ArrayList of Generics.所以我一直在尝试创建一个 ArrayList of ArrayList of Generics。 Basicaly I have a generic class called Color(which takes in Integer/Long/short values at runtime).基本上我有一个名为 Color 的通用类(它在运行时接受整数/长/短值)。 And now I wanted to create an 2d arrayList of type "Color" in lets say a class called Picture.现在我想在一个名为 Picture 的类中创建一个“Color”类型的 2d arrayList。 I've got the constructor and the declaration of the arrayList.我有构造函数和arrayList 的声明。 But what I don't understand is why the size of the ArrayList ends up being 0 even though I declare the size to be greater than 0. More on this after the code...但我不明白的是,为什么 ArrayList 的大小最终为 0,即使我声明它的大小大于 0。代码之后的更多内容......

import java.util.ArrayList;

public class Picture{

    private ArrayList<ArrayList<Color<?>>> pic;
    
    //constructor that takes in height and width of 2d array, and a Color parameter that will be populated in the 2d list
    public Picture(int height, int width, Color color){ 
        
        if((height <= 0) || (width<=0)){
            throw new IndexOutOfBoundsException("invalid height and/or width");
        }
        else{
            pic = new ArrayList<ArrayList<Color<?>>>(height);
            System.out.println("size of given array: "+ pic.size()); //for error handling purposes
            for(int r =0; r< pic.size(); r++ ){
                pic.set(r, new ArrayList<Color<?>>(width));
                for(int c = 0; c< width; c++){
                    pic.get(r).set(c, color);
                }

            } 

        }

    }

}

Everytime time I run the code to test it by creating a new Picture object like Picture pic1 =new Picture(3,4, new Color<Integer>(2));每次我运行代码以通过创建一个新的图片对象来测试它时,如Picture pic1 =new Picture(3,4, new Color<Integer>(2)); or for that matter any values greater than 0 as the height and width;或者就此而言,任何大于 0 的值作为高度和宽度; I always end up getting "size of given array: 0" (from the System.out.println() statement) and the for loop doesn't even take place since the size of the list ends up being 0. Finally the whole population of the Color object that should be taking place in the 2d ArrayList doesn't happen at all and the '2d ArrayList - pic' ends up containing nothing.我总是最终得到“给定数组的大小:0”(来自System.out.println()语句)并且 for 循环甚至没有发生,因为列表的大小最终为 0。最后整个人口应该在 2d ArrayList 中发生的 Color 对象根本没有发生,并且“2d ArrayList - pic”最终什么都不包含。

I dont't understand where I'm going wrong and where the 2d ArrayList is being assigned of size 0, even when I assign it with 'height' a value that is guarenteed to be greater than zero(cause of the if statement).我不明白我哪里出错了以及 2d ArrayList 在哪里分配的大小为 0,即使我为它分配了“高度”一个保证大于零的值(if 语句的原因)。 I'm assuming I declared/initialized the 2d Arraylist - pic wrong.我假设我声明/初始化了 2d Arraylist - pic错误。 Can someone please help me out?有人可以帮我吗? Also feel free to point out any other errors in my code.也可以随意指出我的代码中的任何其他错误。

Thanks谢谢

You misunderstand ArrayList / that constructor.你误解了 ArrayList /那个构造函数。

ArrayLists have two properties that are size-like: ArrayLists 有两个类似大小的属性:

Capacity容量

This number is almost never interesting and means very little.这个数字几乎从不有趣,而且意义很小。 It is an implementation detail: An array of capacity 10 will allow you to add up to 10 elements to it without an internal array resize (what is an internal array resize? an.. internal implementation detail. I told you it's basically irrelevant).这是一个实现细节:容量为 10 的数组将允许您在不调整内部数组大小的情况下最多添加 10 个元素(什么是内部数组调整大小?一个.. 内部实现细节。我告诉过你这基本上无关紧要)。 If you try to add an 11th, that works just fine, and as part of that, the capacity is automatically grown a bit (probably to 15, if memory serves).如果您尝试添加第 11 个,那效果很好,作为其中的一部分,容量会自动增加一点(如果没记错的话,可能会增加到 15)。 Internally, ArrayList has an array with the data (hence the name), and if you attempt to add an item to an arraylist that is at capacity, internally a new, larger array is made, the old array is copied over to the new one, then the new array takes the place of the data store and the old one is discarded.在内部,ArrayList 有一个包含数据的数组(因此得名),如果您尝试将一个项目添加到容量充足的数组列表中,则会在内部创建一个新的更大的数组,旧数组将复制到新数组中,然后新数组取代数据存储,旧数组被丢弃。 That's what capacity is about.这就是容量的意义所在。

There are very few ways to ever even notice, and you rarely need to care.还有,甚至根本注意很少的方式,你很少需要关心。

Size尺寸

This refers to the actual # of entries in the list.这是指列表中的实际条目数。 It is neccessarily capacity or smaller.它必须是capacity或更小。

new ArrayList<Whatever>(10) makes an arraylist of capacity 10 and of size 0. You can't (easily) make an arraylist with X null pointers in it, and you generally don't want to do that either. new ArrayList<Whatever>(10)创建一个容量为 10、大小为 0 的new ArrayList<Whatever>(10)你不能(很容易)创建一个包含 X 个空指针的new ArrayList<Whatever>(10) ,而且你通常也不想这样做。 For example, in this trivial case, all you need to do is [A] replace the r<pic.size() arg with r<height , and to [B] replace .set with .add .例如,在这种微不足道的情况下,您需要做的就是 [A] 将r<pic.size() arg 替换为r<height ,并 [B] 将.set替换为.add

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

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