• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

ArrayIndexOutOfBoundsException error in java  RSS feed

 
Ranch Hand
Posts: 81
Android Chrome Python
  • Mark post as helpful
  • send pies
  • 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?
 
Bartender
Posts: 20743
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • 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: 81
Android Chrome Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 20743
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • 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: 67259
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 20743
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not clarity, it's self-defense:


Like I said, I've been burned.
 
Sheriff
Posts: 21745
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 20743
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • 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: 21745
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 64494
225
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Lasagna is spaghetti flvored cake. Just like this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!