this line of code:
if (fName == (monitorsVector.elementAt(i)).toString())
gives this error:
ArrayIndexOutOfBoundsException: 0>=0
Any idea how to fix that?
Moderators: SecretSquirrel, just brew it!
daveagn wrote:Have you printed out the result of monitorsVector.capacity() to make sure it's returning the correct value?
Yes, it prints out a bunch of "10"'s and then I have it exit.
By this point, there are no objects yet in the vector.
for(int i = 0; i < monitorsVector.capacity(); i++)
{
if (fName.equalsIgnoreCase((monitorsVector.elementAt(i)).toString()))
return ((FileMonitor) monitorsVector.elementAt(i));
} Could it be that java crashes when it tries to change a null (or garbage) to a string?
Interator i = monitorVector.iterator();
FileMonitor o;
while( i.hasNext() )
{
o = (FileMonitor)i.next();
if (fName.equalsIgnoreCase(o.toString())
return o;
}One thing I don't get is why it crashes when it tries to read empty vector cells -- cells that exist, just have nothing in them.
FireGryphon wrote:objects may be removed from the middle of the vector, it may be that a vector of capacity 10 has only three elements in it, with one element at the very end of the vector.
import java.util.Vector;
public class vector
{
public static void printIntegerVector(Vector v)
{
System.out.println("Vector size: " + v.size());
for (int i = 0; i < v.size(); i++)
{ System.out.println("Element " + i + ": " + v.get(i)); }
System.out.println("");
}
public static void main(String args[])
{
Vector v = new Vector();
// Vector with Integers 0, 1, 2, 3, 4
for (int i = 0; i < 5; i++)
{ v.addElement(new Integer(i)); }
printIntegerVector(v);
// Removing the 0th element
v.removeElementAt(0);
printIntegerVector(v);
}
}
Users browsing this forum: No registered users and 1 guest