简体   繁体   中英

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. Basicaly I have a generic class called Color(which takes in Integer/Long/short values at runtime). And now I wanted to create an 2d arrayList of type "Color" in lets say a class called Picture. I've got the constructor and the declaration of the 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...

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)); or for that matter any values greater than 0 as the height and width; 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.

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). I'm assuming I declared/initialized the 2d Arraylist - pic wrong. Can someone please help me out? Also feel free to point out any other errors in my code.

Thanks

You misunderstand ArrayList / that constructor.

ArrayLists have two properties that are size-like:

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). 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). 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. 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.

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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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