I am stuck on a project because I have no clue how to store a picture into an 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. 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. Since Picture
extends Object
you can store it in an array like you would any other 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.
List<Picture> pictures = new ArrayList<Picture>();
pictures.add(pictureToAdd);
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.