Processing 处理:当添加到ArrayList时,PImage将转换为不同类型的对象

Processing 处理:当添加到ArrayList时,PImage将转换为不同类型的对象,processing,Processing,我这里有个奇怪的错误 PImage img; PImage img2; PImage img3; PImage img4; PImage img5; PImage img6; PImage img7; PImage img8; PImage img9; PImage img10; int count; int regionHeight; int regionWidth; ArrayList images; void setup(){ //Image of bottle i

我这里有个奇怪的错误

PImage img; 
PImage img2;
PImage img3; 
PImage img4;
PImage img5; 
PImage img6;
PImage img7; 
PImage img8;
PImage img9; 
PImage img10;
int count;
int regionHeight; 
int regionWidth;
ArrayList images; 

void setup(){
  //Image of bottle 
 images = new ArrayList(); 
 img = loadImage("IMG_3763.JPG");
 images.add(img); 
 img2 = loadImage("IMG_3764.JPG");
 images.add(img2);  
 img3 = loadImage("IMG_3765.JPG");
 images.add(img3); 
 img4 = loadImage("IMG_3766.JPG");
 images.add(img4);  
 img5 = loadImage("IMG_3767.JPG");
 images.add(img5); 
 img6 = loadImage("IMG_3768.JPG");
 images.add(img6);  
 img7 = loadImage("IMG_3769.JPG");
 images.add(img7); 
 img8 = loadImage("IMG_3770.JPG");
 images.add(img8);  
 img9 = loadImage("IMG_3771.JPG");
 images.add(img9); 
 img10 = loadImage("IMG_3772.JPG"); 
 images.add(img10); 

 size(img.width, img.height);
 println(img.width); 
}

void draw(){ 
 println(images.get(0).width); 
}
似乎我放入数组的任何图像都确实是PImage,这就是为什么我可以在setup()中获取img.width,但当我尝试对images.get(index)执行相同操作时,它返回一个对象,但该对象不是PImage。我不知道为什么类型会改变,我的理解是ArrayList就像一个动态数组,具有一些额外的功能。它应该返回一个PImage对象,但处理过程并没有这样对待它。我用不同的函数验证了这一点,以尝试读取更多返回的内容,但它看起来确实是一种不同类型的对象。我错过什么了吗


谢谢

从ArrayList获取对象时,必须将其强制转换为PImage

println((PImage)images.get(0).width);

ArrayList存储对象,它不知道它存储的对象的派生类型,这就是@bill\u automata引用的cast修复问题的原因。

谢谢,修复了它!但仅供参考,这是否意味着对于存储在arraylist中的任何对象,我都必须将其重新转换为某种类型的对象,甚至是基本对象?基本对象为此有特殊的对象,int=Integer,float=float。。。所以它会变成int((整数)arrayListOf_ints.get(idx))