This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 38045
    
  22
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: 7552
    
  18

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: 7552
    
  18

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: 38045
    
  22
Winston said starting at 1.

Of course, the correct starting index is ½
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Trouble with an array.
 
Similar Threads
How can I initialize a Set on creation?
Array dimension missing
array
doubt pg 340 of k&b
array as an object?