Question:
Is there a better option then a 10000 case switch statement?
Zachary
2012-04-06 23:59:03 UTC
what I am trying to do is build an acroynm program in Eclipse for java

example:
user enters: LOL

output would be: Laugh Out Loud

I need to be able to support 10,000 - 12,000 different cases.

If there is a better option please provide a link to a tutorial or example. thanks in advance for your reply.
Four answers:
anonymous
2012-04-07 00:09:08 UTC
With those numbers, it's better to store them in a database like MySQL and let your program retrieve the terms from it. It's a lot less work, more powerful because you can use wildcards in case you don't know the exact acronym, you can generate lists sorted in any way imaginable...
gjmb1960
2012-04-07 00:03:23 UTC
yes there is.

create a map with key the acronym ( LOL ) and value the output:Laugh Out Loud



you will only have to call then map.get(acronym), its either null or the output.
anonymous
2017-01-17 18:04:18 UTC
modern-day compilers will decide on the suited properly matched build to change the build you have written on your code if necessary. case in point, i'm surprisingly constructive maximum Java compilers will emit an if...else bytecode shape it extremely is akin to the change fact on your Java code. some compilers are even smart sufficient to accomplish ordinary refactoring and optimisation of boolean expressions, thoroughly changing the form of if...else statements mutually as conserving the unique semantics and pass of administration. anybody with a ideal training in compiler theory (such because of the fact the compiler dev communities at solar and Microsoft) will evaluate an if...else fact to be considered one of those change, particularly than any incorrect way around, for the reason that even an easy if (without else) is frequently modelled as a change fact by skill of compilers. it fairly is because of the fact the compiler can persist with optimisations to alter constructs much less complicated than can persist with them to if...else constructs. case in point, here 2 instruments of code will consequence in comparable bytecode with basically trivial variations that don't warrant conserving: change (subject) { case authentic: // Do constructive subject stuff ruin; default: // Do unfavourable subject stuff ruin; } ...and... if (subject) { // Do constructive subject stuff } else { // Do unfavourable subject stuff } In maximum languages there are grammatical variations between the two constructs, such as scope regulations battling variables from being declared quickly in a case block (maximum languages require which you declare a clean scope first), yet there are no semantic variations. So, in answer, there's no genuine earnings of utilising one over the different, different than in terms of clarity. The exception is once you start up lacking out ruin statements, and permitting administration to drop by way of from one case to the subsequent; in this occasion the compiler has to generate further bytecode to administration which case blocks ought to be carried out, ensuing in a larger bytecode image and slower execution - to no longer point out the messy, unreadable and unmaintainable source code ;-) in case you're fairly involved to work out the top distinction, you will ought to examine the bytecode your specific compiler emits for equivalent change and if...else constructs.
anonymous
2012-04-07 01:51:43 UTC
yes, it is possible..


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...