Here you can find the source of fillUpWithNulls(List
null
-value entries for every missing intermediate integer value.
Parameter | Description |
---|---|
list | List containing integer values |
public static List<Integer> fillUpWithNulls(List<Integer> list)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Main { /**/*from ww w . j ava 2 s.c o m*/ * Expands an integer list to a size equal to its value range and adds * <code>null</code>-value entries for every missing intermediate integer * value. * * @param list * List containing integer values * @return An expanded list containing null values for missing intermediate * integer values */ public static List<Integer> fillUpWithNulls(List<Integer> list) { return fillUpWithNulls(list, null); } /** * Expands an integer list to a size equal to its value range and adds * <code>null</code>-value entries for every missing intermediate integer * value. If <code>replace</code> is not <code>null</code>, all original * values are replaced by <code>replace</code>. * * @param list * List containing integer values * @param replace * Replacement for existing values within <code>list</code> * @return An expanded list containing null values for missing intermediate * integer values and optionally replaced original values */ public static List<Integer> fillUpWithNulls(List<Integer> list, Integer replace) { Collections.sort(list); int minValue = list.get(0); int maxValue = list.get(list.size() - 1); int range = (int) (maxValue - Math.signum(minValue) * Math.abs(minValue)); List<Integer> result = new ArrayList<>(range + 1); for (int i = 0; i < list.size() - 1; i++) { result.add(replace != null ? replace : list.get(i)); for (int j = 0; j < list.get(i + 1) - list.get(i) - 1; j++) result.add(null); } result.add(replace != null ? replace : maxValue); return result; } }