wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array Indexing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array Indexing" Watch "Array Indexing" New topic
Author

Array Indexing

Srinivasan Krishnan
Greenhorn

Joined: Nov 07, 2000
Posts: 8
Please look in the following program. Is it mandatory to initialize the index variable before accessing any area? The reason I am asking this question is with out reassigning the value of the index variable (here it is 'K') I am getting an error indexoutofbound exception.
Can someone explain, Please...
class GC
{
public static void main(String args[])
{
int i[] = new int[6];
int k = 0;
for(int j = 1000; j < 1006; j++)
{
i[k] = j;
k++;
}
for (int j = 0; j < 6; j++)
{
System.out.println(i[j]);
}
i = new int[10];
k = 0;
for(int j = 1000; j < 1010; j++)
{
i[k] = j;
k++; //Line in question.
}
for(int j = 0; j < 10; j++)
{
System.out.println(i[j]);
}
}
}
Thank you
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
This code works fine for me. This display shows 1000 up to 1005 then 1000 up to 1009.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Srinivasan Krishnan
Greenhorn

Joined: Nov 07, 2000
Posts: 8
Remove the "line in question" and then try the pgm. It gives the exception.
Vidya Selvaraj
Greenhorn

Joined: Nov 21, 2000
Posts: 29
I do not get any exception if I remove k++; It compiles fine & the o/p is 1000 to 1005, 1009, & 9 zeroes
Srinivasan Krishnan
Greenhorn

Joined: Nov 07, 2000
Posts: 8
I apologize. The line that needs to be removed is "K = 0". It is immediately above the For Loop having "K++;". I am getting an error message java.lang.ArrayIndexOutOfBoundsException.
I repeat, my question was should we need to intialize the array index every time?
Vidya Selvaraj
Greenhorn

Joined: Nov 21, 2000
Posts: 29
Hi Srinivasan,
Ok I am assuming that you are referring to K = 0 after i = new int[10];
After the line i = new int[10], the value of K is 6. if you remove k =0, The i[K] in
for(int j = 1000; j < 1010; j++)
{
i[k] = j;
k++; //Line in question.
}
will start with i[6], the for loop is performed 9 times, which will cause k value or i[k] go upto i[14]. Here is the problem. i is defined only as i[10], hope you got the point now!
Thanx
Vidya
[This message has been edited by Vidya Selvaraj (edited December 04, 2000).]
anil bisht
Ranch Hand

Joined: Nov 04, 2000
Posts: 81
yes it is necesary to initilize any variable which are inside a function..
if its a member variable then its not necessary to initialize the variable. i.e the varialbe is directly inside a class and not inside any curly braces.. because then the compiler aotomatically initialize then for u..
try this it will work fine
class GC
{
int k;
public static void main(String args[])
{
int i[] = new int[6];
//int k = 0; this line removed ..
for(int j = 1000; j < 1006; j++)
{
i[k] = j;
k++;
}
for (int j = 0; j < 6; j++)
{
System.out.println(i[j]);
}

hth
anil
Sam Wong
Ranch Hand

Joined: Dec 07, 2000
Posts: 133
Yes, you have to reset k to zero. Here's an alternative approach.
class GC
{
public static void main(String args[])
{
int i[] = new int[6];
for(int j = 1000, k = 0; j < 1006; j++, k++)
{
i[k] = j;
}
for (int j = 0; j < 6; j++)
{
System.out.println(i[j]);
}
i = new int[10];
for(int j = 1000, k = 0; j < 1010; j++, k++)
{
i[k] = j;
}
for(int j = 0; j < 10; j++)
{
System.out.println(i[j]);
}
}
}

This way, you don't need to worry about resetting any used variables which can often cause RuntimeExceptions.
 
wood burning stoves
 
subject: Array Indexing
 
Similar Threads
sorting multidimensional arrays (int)
Sorting multi dimensional array
Confused with pre and Post increment please help ..
confusing for Loop.
Radix Sort in descending order help