Java ArrayList总计数

时间:2015-12-18 15:44:10

标签: java arraylist

我有一个要求,我需要循环遍历Java对象列表并添加到ArrayList。它正确地给出了Java对象的个别计数,但不是所有对象的最终计数。不知道我在哪里做错了。这是输出。预计应该是5945.请帮我解决这个问题。

我尝试将列表声明为静态全局变量,但仍然遇到同样的问题。

categoryEntryList --------->10
categoryEntryList --------->12
categoryEntryList --------->53
categoryEntryList --------->2
categoryEntryList --------->35
categoryEntryList --------->79
categoryEntryList --------->4355
categoryEntryList --------->1
All Category Entries size ------->692

这是代码:

private static List<CategoryEntry> getCategoryMedia(
        List<Category> categoryList) throws KalturaApiException,
        ParseException {
    int index = 1;
    List<CategoryEntry> categoryEntries = new ArrayList<CategoryEntry>();
    List<CategoryEntry> finalEntries = new ArrayList<CategoryEntry>();
    for (Category category : categoryList) {
        CategoryEntryFilter filter = new CategoryEntryFilter();
        FilterPager pager = new FilterPager();
        pager.pageIndex = index;
        pager.pageSize = 500;
        filter.categoryIdEqual = category.id;
        filter.statusEqual = CategoryEntryStatus.ACTIVE;
        CategoryEntryListResponse categoryEntryList = getCategoryEntry(filter, pager);
        if (categoryEntryList.totalCount != 0) {
            System.out.println("categoryEntryList --------->"+categoryEntryList.totalCount);
            for (CategoryEntry entry : categoryEntryList.objects) {
                if (entry != null) {
                    categoryEntries.add(entry);
                }
            }
        }
    }
    index ++;
    finalEntries.addAll(categoryEntries);
    //System.out.println("Total Media ------- >"
    //+ finalEntries.size());
    return finalEntries;
}

protected static CategoryEntryListResponse getCategoryEntry(
        CategoryEntryFilter filter, FilterPager pager)
        throws KalturaApiException {
    CategoryEntryService categoryEntryService = client
            .getCategoryEntryService();
    CategoryEntryListResponse categoryEntryList = categoryEntryService
            .list(filter, pager);
    return categoryEntryList;
}

谢谢, Rajyalakshmi S

2 个答案:

答案 0 :(得分:1)

显然有些元素是null,并且由于条件而未添加到最终列表中。这可能与list(...)实施和/或CategoryFilter filter有关,因为我们没有看到源代码。

答案 1 :(得分:0)

你应该保持一个正在运行的记录

int tally = 0; //declare and assign tally
for (Category category : categoryList) {
    CategoryEntryFilter filter = new CategoryEntryFilter();
    FilterPager pager = new FilterPager();
    pager.pageIndex = index;
    pager.pageSize = 500;
    filter.categoryIdEqual = category.id;
    filter.statusEqual = CategoryEntryStatus.ACTIVE;
    CategoryEntryListResponse categoryEntryList = getCategoryEntry(filter, pager);
    if (categoryEntryList.totalCount != 0) {
        System.out.println("categoryEntryList --------->"+categoryEntryList.totalCount);
        tally += categoryEntryLIst.totalCount; //accumulate tally
        for (CategoryEntry entry : categoryEntryList.objects) {
            if (entry != null) {
                categoryEntries.add(entry);
            }
        }
    }
}
System.out.println("All Category entry size ----------->" + tally); // print tally