Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

arrayOutOfIndex

 
peter brews
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
Could you please let me know what cahnges should i make to print the last value of vector v3.As i am geeting arrayOutOfindex error.
import java.io.*;
import java.util.*;
public class Application1
{
public Application1()
{
}
public static void main(String[] args)
{
new Application1();
Vector v1 = new Vector();
Vector v2 = new Vector();
Vector v3 = new Vector();
v1.addElement("v11");
v1.addElement("v12");
v1.addElement("v13");
v1.addElement("v14");
v2.addElement("v21");
v2.addElement("v22");
v3.addElement("v31");
v3.addElement("v32");
v3.addElement("v33");
v3.addElement("v34");
v3.addElement("v35");
int i = v1.size();
System.out.println("size of v1 is " +i);
int j = v2.size();
System.out.println("size of v2 is " +j);
int k = v3.size();
System.out.println("size of v3 is " +k);
for( int m = 0; m < 4 ; ++m )
{
System.out.println("<tr>");
if( m < ( v1.size()-1) )
{
String value =(((String)v1.elementAt( m+1 ) ));
System.out.println("v1 vaue" +value);
}
if( m < ( v2.size()-1) )
{
String value =(((String)v1.elementAt( m+1 ) ));
System.out.println("v2 vaue" +value);
}
if( m < ( v3.size()-1) )
{
String value =(((String)v1.elementAt( m+1 ) ));
System.out.println("v3 vaue" +value);
}
}
System.out.println("<tr>");
}
}

Following is the result after execution:
size of v1 is 4
size of v2 is 2
size of v3 is 5
<tr>
v1 vauev12
v2 vauev12
v3 vauev12
<tr>
v1 vauev13
v3 vauev13
<tr>
v1 vauev14
v3 vauev14
<tr>
java.lang.ArrayIndexOutOfBoundsException: 4 >= 4
java.lang.Object java.util.Vector.elementAt(int)
void mypackage1.Application1.main(java.lang.String[])
Exception in thread main
Thanks,
smita
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at your code carefully. You're accessing v1 several times when I think you mean to access v2 or v3 instead.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic