willcont
asked on
Creating a Class that throws IndexOutOfBoundsException
Hello
I was wondering if someone can help me create a class that throws an IndexOutOfBoundsException
i.e. like
class ThrowingExcep{
public Object elementAt (int index)throws IndexOutOfBoundsException{
return ___?____ ;}
}
I do not know what to put in the return statement
Thanks in advance
Will
I was wondering if someone can help me create a class that throws an IndexOutOfBoundsException
i.e. like
class ThrowingExcep{
public Object elementAt (int index)throws IndexOutOfBoundsException{
return ___?____ ;}
}
I do not know what to put in the return statement
Thanks in advance
Will
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
import java.util.*;
class StringStore{
Vector v;
public StringStore(){
v = new Vector();
}
public void putString(String s){
v.add(s);
}
public String getString(int index) throws IndexOutOfBoundsException{
if (index > v.size()){
throw new IndexOutOfBoundsException( );
}
return (String)v.elementAt(index) ;
}
}
class StringStore{
Vector v;
public StringStore(){
v = new Vector();
}
public void putString(String s){
v.add(s);
}
public String getString(int index) throws IndexOutOfBoundsException{
if (index > v.size()){
throw new IndexOutOfBoundsException(
}
return (String)v.elementAt(index)
}
}
Woah...everyone posted at once.
Should have reloaded before I hit submit.
Should have reloaded before I hit submit.
ASKER
Thank you
class ThrowingExcep{
{ Vector l;
int max;
ThrowingExcep()
{ l=new Vector();
max=0;
return;
}
public Object elementAt(int index) throws IndexOutOfBoundsException
{ if(index>=max)throw new OutOfBoundsException();
return(l.elementAt(index))
}
}