File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Understanding this Array?

 
Jeremy Parsons
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


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


Bu.
 
omkar patkar
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It just prints 75 now..
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Omkar marked it already in bold type.
What's the difference between
<
and <=
?


Bu.
 
wise owen
Ranch Hand
Posts: 2023
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting the following compile error...
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic