Java tutorial
//package com.java2s; /* * Geotoolkit.org - An Open Source Java GIS Toolkit * http://www.geotoolkit.org * * (C) 2001-2012, Open Source Geospatial Foundation (OSGeo) * (C) 2009-2012, Geomatys * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; * version 2.1 of the License. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. */ import javax.swing.*; import java.util.Arrays; public class Main { /** * Removes the given elements from the given list. This method tries to use * {@link DefaultListModel#removeRange} when possible in order to group events * together. * <p> * <strong>Warning:</strong> This method override the given {@code indices} array. * * @param list The list from which to remove elements. * @param indices The index of elements to remove. */ public static void remove(final DefaultListModel<?> list, final int[] indices) { // We must iterate in reverse order, because the // index after the removed elements will change. int i = indices.length; if (i != 0) { Arrays.sort(indices); int upper = indices[--i]; int lower = upper; while (i != 0) { int previous = indices[--i]; if (previous != lower - 1) { if (lower == upper) { list.remove(lower); } else { list.removeRange(lower, upper); } upper = previous; } lower = previous; } if (lower == upper) { list.remove(lower); } else { list.removeRange(lower, upper); } } } }