aspose file tools*
The moose likes Beginning Java and the fly likes arrayOutOfIndex Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "arrayOutOfIndex" Watch "arrayOutOfIndex" New topic
Author

arrayOutOfIndex

peter brews
Ranch Hand

Joined: Jan 25, 2002
Posts: 59
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

Joined: Jan 30, 2000
Posts: 18671
Look at your code carefully. You're accessing v1 several times when I think you mean to access v2 or v3 instead.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: arrayOutOfIndex