• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Understanding this Array?

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using what I learned about arrays and have made the following program...

now I understand that int[] price = {25,75}; is creating two memory locations for 25 and 75.. I know the for statement seems right, but I can't get it to print, or know if I am doing this right.. Any help please?
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


replace the line price[sub] += 25;
by System.out.println(sub);
and you'll see.


Bu.
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI jeremy,
If you want to print the values in the two locations after you have added 25 .... then i think you can use the for loop as :-



This should print 50 and 100 !
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It just prints 75 now..
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Omkar marked it already in bold type.
What's the difference between
<
and <=
?


Bu.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
15.20.1 Numerical Comparison Operators <, <=, >, and >=
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use i=0; i<=1. Use i=0; i<2. The "2" is the size of the array, and once you're used to it, it is completely natural. The first way is confusing because it's not the standard way Java programmers do things.
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm getting the following compile error...
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ernest wrote:

Don't use i=0; i<=1. Use i=0; i<2. The "2" is the size of the array, and once you're used to it, it is completely natural. The first way is confusing because it's not the standard way Java programmers do things.


Absolutely. But in case of beginners it's ok to experiment around.
Just before they start using the arraysName.length variable. The prefix increment in this for loop is also a bit unusual in my view.
Jeremy, how about this:


And Jeremy wrote:

I'm getting the following compile error...


You just forgot the number/variable in the [ ].



Yours,
Bu.
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ran it, but am getting:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at Price.main(Price.java:8)
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops,


sorry, my fault...
not
for(sub =0; sub<=price.length; sub++)
but
for(sub =0; sub<price.length; sub++)

as length is two and the indices of your array are 0 and 1.
length is two for two elements in your array, that's one two much.
Compiler can't check when we use to big or to low indices, so an exception at runtime is produced.

Sorry,
Bu.
reply
    Bookmark Topic Watch Topic
  • New Topic