Java List Count countAtLevel(List aList, int aLevel)

Here you can find the source of countAtLevel(List aList, int aLevel)

Description

Returns the number of objects at a given level in the given list hierarchy.

License

Open Source License

Declaration

public static int countAtLevel(List aList, int aLevel) 

Method Source Code


//package com.java2s;

import java.util.*;

public class Main {
    /**// www . j a  va 2  s . c  o  m
     * Returns the number of objects at a given level in the given list hierarchy.
     */
    public static int countAtLevel(List aList, int aLevel) {
        if (aLevel < 0)
            return 1;

        if (aLevel > 0) {
            int count = 0;
            for (int i = 0, iMax = aList.size(); i < iMax; i++)
                count += countAtLevel((List) aList.get(i), aLevel - 1);
            return count;
        }

        return aList.size();
    }

    /**
     * Returns the size of a list (accepts null list).
     */
    public static int size(List aList) {
        return aList == null ? 0 : aList.size();
    }

    /**
     * Returns the object at the given index (returns null object for null list or invalid index).
     */
    public static <T> T get(List<T> aList, int anIndex) {
        return aList == null || anIndex < 0 || anIndex >= aList.size() ? null : aList.get(anIndex);
    }
}

Related

  1. count(List l, int from, int to, Object what)
  2. count(List values)
  3. count(List lines)
  4. countNumber(List list, int number)
  5. countOccurances(List encodings, int value)
  6. countOccurence(List valueList, int value)
  7. countOccurrencesOf(Object comparisonObject, List list)