wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Understanding this Array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Understanding this Array?" Watch "Understanding this Array?" New topic
Author

Understanding this Array?

Jeremy Parsons
Ranch Hand

Joined: Apr 21, 2006
Posts: 74
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

Joined: Aug 25, 2006
Posts: 1274
Hi,


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


Bu.


all events occur in real time
omkar patkar
Ranch Hand

Joined: Aug 25, 2005
Posts: 231
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 !


Thanks and Regards
Omkar Patkar (SCJP 1.4)[url]http://javacollectionsnotes.blogspot.com[/url] | [url]http://omkar-myscjpexp.blogspot.com[/url]
Jeremy Parsons
Ranch Hand

Joined: Apr 21, 2006
Posts: 74
It just prints 75 now..
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Omkar marked it already in bold type.
What's the difference between
<
and <=
?


Bu.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
15.20.1 Numerical Comparison Operators <, <=, >, and >=
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Jeremy Parsons
Ranch Hand

Joined: Apr 21, 2006
Posts: 74
I'm getting the following compile error...
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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

Joined: Apr 21, 2006
Posts: 74
I ran it, but am getting:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at Price.main(Price.java:8)
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Understanding this Array?
 
Similar Threads
problem with parse int method
input is showing different than output
overriding & instance variable
trying to get a value from another class
If Statement Help