您需要的是一个动态数据结构,如 List。
您可以使用通用(即列表)或非通用(即列表)版本。使用列表,您可以动态添加或插入项目,确定它们的索引并根据需要删除项目。
当您使用列表操作时,列表的大小会动态增长/缩小。
假设您的图像表示为 Image 类型的对象,那么您可以使用这样的 List :
// instantiation of an empty list
List<Image> list = new List<Image>();
// create ten images and add them to the list (append at the end of the list at each iteration)
for (int i = 0; i <= 9; i++) {
Image img = new Image();
list.Add(img);
}
// remove every second image from the list starting at the beginning
for (int i = 0; i <= 9; i += 2) {
list.RemoveAt(i);
}
// insert a new image at the first position in the list
Image img1 = new Image();
list.Insert(0, img1);
// insert a new image at the first position in the list
IMage img2 = new Image();
list.Insert(0, img2);
使用字典的替代方法:
Dictionary<string, Image> dict = new Dictionary<string, Image>();
for (int i = 0; i <= 9; i++) {
Image img = new Image();
// suppose img.Name is an unique identifier then it is used as the images keys
// in this dictionary. You create a direct unique mapping between the images name
// and the image itself.
dict.Add(img.Name, img);
}
// that's how you would use the unique image identifier to refer to an image
Image img1 = dict["Image1"];
Image img2 = dict["Image2"];
Image img3 = dict["Image3"];