• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Indexing

 
Srinivasan Krishnan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code works fine for me. This display shows 1000 up to 1005 then 1000 up to 1009.
 
Srinivasan Krishnan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remove the "line in question" and then try the pgm. It gives the exception.
 
Vidya Selvaraj
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic