raidmax
Apr 7, 2015, 11:41 AM
Hi I'm getting index out of bound with the code below but I cannot find exactly what is causing the error.can anybody help.tnks
import java.util.*;
class allshortinVector
{
public static void main(String[] args)
{
Vector<String> v=new Vector();
v.addElement("test");
v.addElement("hello");
v.addElement("JAVA");
v.addElement("Program");
Vector<String> v2=new Vector();
for(int I=0 ; I < v.size() ; I++)
{
if( v.elementAt(I).length() < v.elementAt(I + 1).length() ) // checking which element is the longest
{
v2.addElement(v.elementAt(I)); // store them in vector v2
}
}
for(String z : v2)
System.out.println(z);
}
}
import java.util.*;
class allshortinVector
{
public static void main(String[] args)
{
Vector<String> v=new Vector();
v.addElement("test");
v.addElement("hello");
v.addElement("JAVA");
v.addElement("Program");
Vector<String> v2=new Vector();
for(int I=0 ; I < v.size() ; I++)
{
if( v.elementAt(I).length() < v.elementAt(I + 1).length() ) // checking which element is the longest
{
v2.addElement(v.elementAt(I)); // store them in vector v2
}
}
for(String z : v2)
System.out.println(z);
}
}