Do range checking in remove(int index).
git-svn-id: https://samskivert.googlecode.com/svn/trunk@1179 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// $Id: SortableArrayList.java,v 1.16 2003/07/15 00:31:54 ray Exp $
|
// $Id: SortableArrayList.java,v 1.17 2003/07/29 23:44:14 ray Exp $
|
||||||
//
|
//
|
||||||
// samskivert library - useful routines for java programs
|
// samskivert library - useful routines for java programs
|
||||||
// Copyright (C) 2001 Michael Bayne
|
// Copyright (C) 2001 Michael Bayne
|
||||||
@@ -194,48 +194,36 @@ public class SortableArrayList extends AbstractList
|
|||||||
// documentation inherited from interface
|
// documentation inherited from interface
|
||||||
public Object get (int index)
|
public Object get (int index)
|
||||||
{
|
{
|
||||||
if (index >= 0 && index < _size) {
|
rangeCheck(index, false);
|
||||||
return _elements[index];
|
return _elements[index];
|
||||||
} else {
|
|
||||||
throw new IndexOutOfBoundsException();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// documentation inherited from interface
|
// documentation inherited from interface
|
||||||
public Object set (int index, Object element)
|
public Object set (int index, Object element)
|
||||||
{
|
{
|
||||||
if (index >= 0 && index < _size) {
|
rangeCheck(index, false);
|
||||||
Object old = _elements[index];
|
Object old = _elements[index];
|
||||||
_elements[index] = element;
|
_elements[index] = element;
|
||||||
return old;
|
return old;
|
||||||
} else {
|
|
||||||
throw new IndexOutOfBoundsException();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// documentation inherited from interface
|
// documentation inherited from interface
|
||||||
public void add (int index, Object element)
|
public void add (int index, Object element)
|
||||||
{
|
{
|
||||||
if (index >= 0 && index <= _size) {
|
rangeCheck(index, true);
|
||||||
_elements = ListUtil.insert(_elements, index, element);
|
_elements = ListUtil.insert(_elements, index, element);
|
||||||
_size++;
|
_size++;
|
||||||
} else {
|
|
||||||
throw new IndexOutOfBoundsException();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// documentation inherited from interface
|
// documentation inherited from interface
|
||||||
public Object remove (int index)
|
public Object remove (int index)
|
||||||
{
|
{
|
||||||
Object removed = ListUtil.remove(_elements, index);
|
rangeCheck(index, false);
|
||||||
if (removed != null) {
|
return ListUtil.remove(_elements, index);
|
||||||
_size--;
|
|
||||||
}
|
|
||||||
return removed;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// documentation inherited from interface
|
// documentation inherited from interface
|
||||||
public int indexOf (Object o)
|
public int indexOf (Object o)
|
||||||
{
|
{
|
||||||
return ListUtil.indexOfEqual(_elements, o);
|
return ListUtil.indexOfEqual(_elements, o);
|
||||||
}
|
}
|
||||||
@@ -245,6 +233,19 @@ public class SortableArrayList extends AbstractList
|
|||||||
// {
|
// {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check the range of a passed-in index to make sure it's valid.
|
||||||
|
*
|
||||||
|
* @param insert if true, an index equal to our size is valid.
|
||||||
|
*/
|
||||||
|
protected void rangeCheck (int index, boolean insert)
|
||||||
|
{
|
||||||
|
if ((index < 0) || (index > _size) || (!insert && index == _size)) {
|
||||||
|
throw new IndexOutOfBoundsException(
|
||||||
|
"Index: " + index + ", Size: " + _size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// documentation inherited
|
// documentation inherited
|
||||||
public Object clone ()
|
public Object clone ()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user