Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

For-loops  RSS feed

 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a short code segment that declares an integer array of 10 elements. Write the statements
required to initialize the first array element to a value of 10 and each succeeding element to a
value three times the previous element value. Use a for loop to perform this action.
This is the correct code:


The only thing I don't understand is why in the for loop conditional statement it is a.length-1. Why can't it be a.length?
 
Sheriff
Posts: 13147
218
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you change it to a.length? Why do you think that happens?
 
Bartender
Posts: 2163
92
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding to what Junilu asked you to do..

Ana Yo wrote:...This is the correct code:


It is not the correct code, there are compilation errors in it. It also does not declare an Integer array with 10 elements.
As per https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html,
only allocates memory for 10 integers.
 
Marshal
Posts: 63314
205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:. . . The only thing I don't understand is why in the for loop conditional statement it is a.length-1. Why can't it be a.length?

Who said it should be a.length − 1? As Junilu has hinted, what is the result when you print the resultant array with Arrays#toString()?
 
Sheriff
Posts: 5742
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:The only thing I don't understand is why in the for loop conditional statement it is a.length-1. Why can't it be a.length?


This line creates an array of 10 elements in indices 0 through 9, because the indices of an array in Java start at 0.  Does this make it clearer?
 
salvin francis
Bartender
Posts: 2163
92
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pardon me for digressing from the original topic ...

yesterday, I wrote wrote:
only allocates memory for 10 integers.


This is not entirely correct. And I learnt something new too

Here's a bit more detail about this behavior (its a 6 year old thread !!):
https://coderanch.com/t/594038/java/local-array-array-initializes

Specifically Jesper de Jong's post that says ,  

... each component of the array is initialized to its default value

Special thanks to Campbell for pointing this out  
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!