首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

准确删除List中的整数值

2013-07-01 
正确删除List中的整数值List为移除数据提供了两个remove的方法:1. 按照下标索引删除2. 按照值删除,删除第

正确删除List中的整数值

List为移除数据提供了两个remove的方法:
1. 按照下标索引删除
2. 按照值删除,删除第一个符合的值对象。

public interface List<E> extends Collection<E> {    //省略其它方法,只保留remove方法    /**     * Removes the first occurrence of the specified element from this list,     * if it is present (optional operation).  If this list does not contain     * the element, it is unchanged.  More formally, removes the element with     * the lowest index <tt>i</tt> such that     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>     * (if such an element exists).  Returns <tt>true</tt> if this list     * contained the specified element (or equivalently, if this list changed     * as a result of the call).     *     * @param o element to be removed from this list, if present     * @return <tt>true</tt> if this list contained the specified element     * @throws ClassCastException if the type of the specified element     *         is incompatible with this list (optional)     * @throws NullPointerException if the specified element is null and this     *         list does not permit null elements (optional)     * @throws UnsupportedOperationException if the <tt>remove</tt> operation     *         is not supported by this list     */    boolean remove(Object o);    /**     * Removes the element at the specified position in this list (optional     * operation).  Shifts any subsequent elements to the left (subtracts one     * from their indices).  Returns the element that was removed from the     * list.     *     * @param index the index of the element to be removed     * @return the element previously at the specified position     * @throws UnsupportedOperationException if the <tt>remove</tt> operation     *         is not supported by this list     * @throws IndexOutOfBoundsException if the index is out of range     *         (<tt>index &lt; 0 || index &gt;= size()</tt>)     */    E remove(int index);}


当一个List存储的是整形数据的时候,需要注意下,以保证是按值删除数据或者按照下标索引删除数据。

public class Test {public static void main(String[] args) {List<Integer> list = new ArrayList<Integer>();list.add(1);list.add(3);list.add(20);list.add(6);list.add(5);list.add(1);/** * 打印List中的值 --> [1, 3, 20, 6, 5, 1] */System.out.println(list);/** * 移动index =1,也就是第二个整数,移除的是3,而不是1。 * 输出List中的值得-->[1, 20, 6, 5, 1] */list.remove(1);System.out.println(list);/** * 移动第一个值为1的整数。 * 输出List中的值得-->[20, 6, 5, 1] */list.remove(Integer.valueOf(1));System.out.println(list);}}


删除整数可以用Integer.valueOf(value)或者 new Integer(value).

最好还是用Integer.valueOf(value), 因为Integer把-128~127的整数放入了cache,cache中取得的值不需要创建新的对象。

public final class Integer extends Number implements Comparable<Integer> {   //其它变量和方法省略static final Integer cache[] = new Integer[-(-128) + 127 + 1];static {    for(int i = 0; i < cache.length; i++)cache[i] = new Integer(i - 128);}    /**     * Returns a <tt>Integer</tt> instance representing the specified     * <tt>int</tt> value.     * If a new <tt>Integer</tt> instance is not required, this method     * should generally be used in preference to the constructor     * {@link #Integer(int)}, as this method is likely to yield     * significantly better space and time performance by caching     * frequently requested values.     *     * @param  i an <code>int</code> value.     * @return a <tt>Integer</tt> instance representing <tt>i</tt>.     * @since  1.5     */    public static Integer valueOf(int i) {final int offset = 128;if (i >= -128 && i <= 127) { // must cache     return IntegerCache.cache[i + offset];}        return new Integer(i);    }}

热点排行