• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trouble with an array.

 
Shelby Simpson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10257
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shelby Simpson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes, thanks. The zero aspect has also been the cause of many headaches.
 
Winston Gutkowski
Bartender
Pie
Posts: 10257
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what would that problem be Winston sir? What could possibly go wrong if I implemented an array with starting index position as "0"?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston said starting at 1.

Of course, the correct starting index is ½
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic