• 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

For-loops  RSS feed

 
Ranch Hand
Posts: 79
  • 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: 12815
211
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?
 
Saloon Keeper
Posts: 2086
76
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: 62231
193
  • 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()?
 
Marshal
Posts: 5633
147
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
Saloon Keeper
Posts: 2086
76
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  
 
Get out of my mind! Look! A tiny ad!
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!