Java集合remove重载方法越界问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chenbetter1996/article/details/88147382

Java集合,如List,有remove方法,但是这是一对重载方法

package xyz.cglzwz.question_bank.collectionandmap;

import java.util.ArrayList;
import java.util.List;

public class ListDemo {

	public static void main(String[] args) {
		List list = new ArrayList();
		
		for (int i = 5; i < 10; ++i) { 
			list.add(i);
		}
		
		for (int i = 5; i < 10; ++i) { 
			list.remove(i);
		}
		
		System.out.println(list);
	}
}
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 5
	at java.util.ArrayList.rangeCheck(ArrayList.java:657)
	at java.util.ArrayList.remove(ArrayList.java:496)
	at xyz.cglzwz.question_bank.collectionandmap.ListDemo.main(ListDemo.java:16)

这个会直接报错,因为这里调用的remove参数为int是索引值。

看看这两个重载方法源码


    /**
     * 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);




    /**
     * 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
     * (<a href="Collection.html#optional-restrictions">optional</a>)
     * @throws NullPointerException if the specified element is null and this
     *         list does not permit null elements
     * (<a href="Collection.html#optional-restrictions">optional</a>)
     * @throws UnsupportedOperationException if the <tt>remove</tt> operation
     *         is not supported by this list
     */
    boolean remove(Object o);
  1. 第二个是移除值为参数的元素,必须绝对不能是基本类型的int,其余都可以。
  2. 比如如上程序直接 remove(3.14)也是不会报错的,即便没有插入这个元素。
  3. 如果List是真想放整型,而且希望remove方法删除参数为元素,则可以使用包装类 remove(new Integer(value))

补充

  1. length这个属性是对于基本类型数组的,数组没有length()和size()方法
  2. length()方法是对于字符串的,字符串没有length属性,也没有size()方法
  3. size()方法是对于集合框架的,集合没有length属性,也没有size()方法
int[] a = new int[5];
int ilen = a.length;


String s = "hello";
int slen = s.length();


List l = new ArrayList();
int llen = l.size();

猜你喜欢

转载自blog.csdn.net/chenbetter1996/article/details/88147382