Results 1 to 3 of 3
Thread: problems with array index
- 08-01-2007, 04:57 AM #1
Member
- Join Date
- Jul 2007
- Posts
- 51
- Rep Power
- 0
problems with array index
I have a vector
I try to print out the smallest number After compiling I get this messege: Arrays index is out of bound Exception.
what's the problem?
Java Code:class VectorTest{ public static void main(String[]args){ int smallestvalue=Integer.MAX_VALUE; int lastvalue; int currentvalue; int []v={5,3,6,0}; int i=0; while(i>=0){ currentvalue= v[i+1]; lastvalue= v[ i ]; if(current>last){ smallestvalue=lastvalue; }else{ smallestvalue=currentvalue; } i++; } System.out.println(smallest); } }
- 08-01-2007, 05:23 AM #2
Senior Member
- Join Date
- Jul 2007
- Posts
- 135
- Rep Power
- 0
You are running off the end of the vector. Instead of a while loop try a for loop like so
Java Code:for (int i = 0; I < v.length; i++) do your checking here;
- 08-01-2007, 04:30 PM #3
Member
- Join Date
- Jul 2007
- Posts
- 51
- Rep Power
- 0
Similar Threads
-
Help with Flesch Index
By L_22 in forum Advanced JavaReplies: 1Last Post: 03-31-2008, 05:30 PM -
Replacing at an index
By bugger in forum New To JavaReplies: 2Last Post: 01-29-2008, 06:33 AM -
z-Index problem
By mjdousti in forum AWT / SwingReplies: 1Last Post: 12-29-2007, 01:34 AM -
problems with asigning elements of an array to a constructor
By rednessc in forum New To JavaReplies: 1Last Post: 12-14-2007, 07:25 AM -
Accessing index.dat files
By vissu007 in forum NetworkingReplies: 1Last Post: 07-01-2007, 04:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks