【问题标题】:unable to retrieve values from two different arraylist无法从两个不同的数组列表中检索值
【发布时间】:2018-11-14 12:02:40
【问题描述】:

我有两个不同大小的数组列表,我的问题是我必须从两个列表中检索值并将其显示到文本视图上,当我使用两个循环时,它首先完成内部循环,然后执行外循环和 vales 被打印两次,如果使用 break 中断内循环,则在第一次循环后完全忽略内循环。

ArrayList<LocationDto>location = new ArrayList<>();
education<EducationDto> = new ArrayList<>();

if (education.size() != 0) {
    for (int j = 0; j < education.size(); j++) {
        for (int k = 0; k < location.size(); k++) {
            if (!education.get(j).getSpecializationTitle().equalsIgnoreCase("") && !location.get(k).getLocationName().equalsIgnoreCase("")) {
                tvEducation.append(education.get(j).getEducationTitle() + "(" + education.get(j).getSpecializationTitle() + ")" + " Located at " + location.get(k).getLocationName());
            } else if (education.get(j).getSpecializationTitle().equalsIgnoreCase("") && !location.get(k).getLocationName().equalsIgnoreCase("")) {
                tvEducation.append(education.get(j).getEducationTitle() + " Located at " + location.get(j).getLocationName());
            } else if (!education.get(j).getSpecializationTitle().equalsIgnoreCase("") && location.get(k).getLocationName().equalsIgnoreCase("")) {
                tvEducation.append(education.get(j).getEducationTitle() + "(" + education.get(j).getSpecializationTitle() + ")");
            } else {
                tvEducation.append(education.get(j).getEducationTitle());
            }
            if (j != education.size() - 1) {
                tvEducation.append(" , ");
            }
            break;
        }
    }
} else {
    tvEducation.setText("Not Specified");
    tvEducation.setTextColor(getResources().getColor(R.color.color_three));
}

我现在该怎么办?

【问题讨论】:

  • 您有什么要求?
  • 首先检查列表的大小。还要将此tvEducation.append(education.get(j).getEducationTitle() + " Located at " + location.get(j).getLocationName()); 更改为tvEducation.append(education.get(j).getEducationTitle() + " Located at " + location.get(k).getLocationName());

标签: android loops arraylist


【解决方案1】:

break 语句会终止一个循环,无论它是否已完成,所以您的问题是您在没有某种条件的情况下直接在内部 for 循环中放置了一个 break。因此,循环第一次运行时,它将到达中断,这就是它在第一次迭代时终止的原因!

你可能是想把 break 放在最后一个 if 语句中?

【讨论】:

    猜你喜欢
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多