我有两个ArrayList,我想通过添加它们来制作一个ArrayList,两个列表的大小相同

我将以这种方式进行。

是否优化了列表,或者当列表变大时我可以使其更好和更有效吗?


    private ArrayList<Bitmap> imageFile= new ArrayList<Bitmap>();

    imageFile.add(xy);
    imageFile.add(ab);
    imageFile.add(cd);

    private ArrayList<MediaPlayer> musicFile= new ArrayList<MediaPlayer>();

    musicFile.add(mm);
    musicFile.add(nn);
    musicFile.add(ll);

    private HashMap<Bitmap, MediaPlayer> mappedFiles= new HashMap<Bitmap, MediaPlayer>();

    mappedFiles.put(imageFile.get(i),musicFile.get(i))


    private ArrayList<HashMap<Bitmap, MediaPlayer>> imageMusic= new ArrayList<HashMap<Bitmap, MediaPlayer>>();

   imageMusic.add(mappedFiles);

最佳答案

根据您的评论,您根本不需要地图,需要类和列表:

public class Track {
    private final String name;
    private final MediaPlayer music;
    public Track (String name, MediaPlayer music) {
        this.name = name;
        this.music = music;
    }
    // getters omitted
}

public class CD {
    private final String name;
    private final BitMap image;
    private final List<Track> tracks = new ArrayList<Track>();
    public CD (String name, BitMap image) {
        this.name = name;
        this.image = image;
    }
    public List<Track> getTracks() {
        return tracks;
    }
    // other getters omitted
}

然后
List<CD> cds = new List<CD>();
CD cd = new CD("Thriller", someBitMap);
cd.getTracks().add(new Track("I'm bad", someMusic));
cds.add(cd);

09-11 18:50