2 Replies - 157 Views - Last Post: 22 January 2013 - 09:07 AM Rate Topic: -----

#1 dasker  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 10-November 10

Question regarding arrays

Posted 22 January 2013 - 09:02 AM

I have a program that I am wrote and i want to assign each element in the array the same value as the index. For some reason it will not work and I am not sure why. Here is a snippet of the for loop going through the array of 6 ints and the return.
I already declared the variable i and intitialized it what is wrong?


 int[] arrayOfIndexes = new int[5];
  
 for(int i = 0; i < 6; i++) { 
  arrayOfIndexes[i] = i;
 
}
  return i;




Is This A Good Question/Topic? 0
  • +

Replies To: Question regarding arrays

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,088
  • Joined: 27-December 08

Re: Question regarding arrays

Posted 22 January 2013 - 09:05 AM

The variable i is local to the for loop, so you cannot access it outside of the loop. Declare i before the loop and initialize it there to solve your problem. I'm not sure why you don't just return the length of the array though.
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5625
  • View blog
  • Posts: 12,061
  • Joined: 02-June 10

Re: Question regarding arrays

Posted 22 January 2013 - 09:07 AM

Your loop does 6 iterations. But you initialized the array for 5 elements (line 1)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1