简体   繁体   English

如何将图像添加到阵列?

[英]How do you add images to an array?

I am stuck on a project because I have no clue how to store a picture into an array . 我陷入了一个项目,因为我不知道如何将图片存储到array

I think it would go in this method 我认为这种方法会

public Picture addPicture (Picture pictureAdded) {

  pRef = pictureAdded;
  return pRef;

}

you can store it just like you'd store any other object in an array 您可以像将任何其他对象存储在数组中一样存储它

Picture [] parr = new Picture[5];
parr[0] = pictureAdded

There are two ways to do it: 有两种方法可以做到这一点:

Picture[] pictures = new Picture[5]; //This will make an array of 5 pictures
pictures[0] = pictureAdded;

You can also use a pre-made class in Java - ArrayList. 您还可以在Java中使用预制类-ArrayList。 This way you don't have to worry about array size: 这样,您不必担心数组大小:

ArrayList<Picture> pictures = new ArrayList<Picture>();
pictures.add(new Picture());
Picture pic = pictures.get(0); 

Java is object-oriented , which lets you take advantage of polymorphism to reuse code and concepts. Java是面向对象的 ,它使您可以利用多态性来重用代码和概念。 Since Picture extends Object you can store it in an array like you would any other object. 由于Picture扩展了Object您可以像存储任何其他对象一样将其存储在数组中。

Picture[] pictures = new Picture[10];
pictures[0] = pictureToAdd;

If you want to iterate through the array you can do: 如果要遍历数组,可以执行以下操作:

for(Picture picture : pictures)
{
    // do stuff here
}

You can also put Picture objects into an ArrayList or any other data structure. 您还可以将Picture对象放入ArrayList或任何其他数据结构中。

List<Picture> pictures = new ArrayList<Picture>();
pictures.add(pictureToAdd);

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

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