aspose file tools*
The moose likes Beginning Java and the fly likes Trouble with an array. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble with an array." Watch "Trouble with an array." New topic
Author

Trouble with an array.

Shelby Simpson
Greenhorn

Joined: Feb 17, 2013
Posts: 26
I have declared an Array like this int[] hourCounts; and in my constructor class I created one - new int[24]. What method do I use to retrieve the number in a given array. Any help would be greatly appreciated.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39826
    
  28
Welcome to the Ranch
Before getting anything out of an array, you have to put it in. Is this Java Tutorials section any use to you?
At least with int[]s and other primitive arrays you get something vaguely useful out if you haven’t put anything in: 0. If you have an array of reference types and don’t put anything in, you get null back. And nulls can have nasty side‑effects.

I have had a look at that Java Tutorials link, and I think it will help you.
Shelby Simpson
Greenhorn

Joined: Feb 17, 2013
Posts: 26
Thanks, my instructor pointed me to this forum. With an instructor that is almost inaccessible it is good have a resource like this one.

I figured out what I was doing wrong. I was looking for a method to retrieve the int value from my array when all I needed was int IndexValue = hoursCount[5]; To get the value of the index at 5 in my array.

Thanks for the help
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8207
    
  23

Shelby Simpson wrote:To get the value of the index at 5 in my array.

And remember that that's actually the 6th element.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Shelby Simpson
Greenhorn

Joined: Feb 17, 2013
Posts: 26
Ah yes, thanks. The zero aspect has also been the cause of many headaches.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8207
    
  23

Shelby Simpson wrote:Ah yes, thanks. The zero aspect has also been the cause of many headaches.

Don't worry, you'll learn to love it - probably by trying to implement a 1-based array at some point, and running into all the problems that made most language designers choose 0.

Winston
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

And what would that problem be Winston sir? What could possibly go wrong if I implemented an array with starting index position as "0"?


~ Mansukh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39826
    
  28
Winston said starting at 1.

Of course, the correct starting index is ½
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble with an array.