• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

ArrayIndexOutOfBoundsException error in java

 
Ranch Hand
Posts: 83
Android Python Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made a Deck class in java. the point of the Deck class is to manage a set of Cards. In the constructor of the Deck class, it generates a list of Cards that make up the Deck. I am getting an error that is as follows:

The code for the Deck class is
and the code for the main class is

Why am I getting this error?
 
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you have suits[a], ranks[b] where you should have written suits[b], ranks[a].

Also, you might find this syntax easier to work with:


The use of i, j, and k as indexes is a carryover convention from common mathematics usage, although for all Java cares, you could use "fred' and "bill" (though Fred and Bill would violate the capitalization conventions).

The i = 0; i < something.length syntax plays to the fact that Java uses zero-based indexes on arrays. Note that by using "<" instead of "<=" you don't need to remember to add the "-1".

Also, I'm snippy about lots of curly brackets myself. I've been burned when I inserted extra statements in a loop or conditional that didn't have them. It's not like we're still saving code on 80 KB floppies.
 
Rocky Rocha
Ranch Hand
Posts: 83
Android Python Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much! I guess I was just so tired I couldn't realize that is was such a simple error. Also, to respond to your comments about syntax, I still code for efficiency. Unnecessary characters are unnecessary.
 
Tim Holloway
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NEVER code for efficiency. You can always make a well-coded program better (if you need to), but a prematurely-optimized program is not only much more difficult to maintain, it's also much more difficult to re-optimize when it turns out that the original design wasn't as optimal as you thought. If you don't believe me, ask anyone in our Optimization forum. Or consult a good book on best practices in software engineering.

In any event, use of spaces, comments, blank lines, brackets, etc. has zero effect on code efficiency. All that stuff is stripped out early in the lexical processing phase of the compiler. The only "efficiency" you'll gain there is whatever keystrokes you save and I doubt there's any place on Earth that will reward you for saving keystrokes.

Well, actually, there probably is, since the insanity and inanity of shop standards knows no bounds, but I sincerely hope I never walk into one of those places.
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rocky Rocha wrote:Unnecessary characters are unnecessary.


Except that the "extra" characters for clarity are not unnecessary.

And, what Tim said.
 
Tim Holloway
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not clarity, it's self-defense:


Like I said, I've been burned.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:I think you have suits[a], ranks[b] where you should have written suits[b], ranks[a].

Also, you might find this syntax easier to work with:


Using for-each loops makes it even easier, and there is no risk of getting your indexes mixed up:
 
Tim Holloway
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point, although I usually go even one step further:


Hope I got the syntax right. I spent too many years using C/C++ enums and there are some differences. Using enums makes things typesafe in addition to providing extra flexibility in calculations and formatting.
 
Rob Spoor
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apart from the semi-colons after the enums those are just fine. Although I'd put Ace after King
 
Marshal
Posts: 79956
396
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rocky Rocha wrote:. . . The code for the Deck class is . . .

Have a look at the old Sun style guide, which tells you that sort of code is poor style. Write this instead:-The () around == are redundant: read redundant as meaning optional rather than unnecessary.
 
Do not set lab on fire. Or this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic