Java嵌套循环中容易出现的问题

来源:互联网 发布:网络监控摄像头序列号 编辑:程序博客网 时间:2024/05/21 10:06
  • 现象

    总是加载数据的总集合大小为0,后来经过断点调试才发现原因: i 和 j 不能混淆

    for (int i = 0; i < songsTypes.size(); i++) {SongsType songsType = songsTypes.get(i);int mCurrentId = songsType.getId();MusicSortItem musicSortItem = new MusicSortItem();musicSortItem.setmTitle(songsType.getType_name());musicSortItem.setType(true);allItems.add(musicSortItem);for (int j = 0; j < songSheetBeanList.size();j++) {    SongSheetBean ssb = songSheetBeanList.get(j);    if (mCurrentId == ssb.getType_id()) {        MusicSortItem musicSortItemSub = new MusicSortItem();        musicSortItemSub.setmTitle(ssb.getSSheet());        musicSortItemSub.setType(false);        musicSortItemSub.setShid(ssb.getShid());        allItems.add(musicSortItemSub);    }}}
  • 需要注意

    嵌套循环外层和层的循环,不能混淆颠倒位置

阅读全文
0 0