im making a java mobile app to display quotes by people. It would hav开发者_StackOverflow社区e over 18000 quotes, categorized by person and number of quotes. Users of my app would enter the person number and quote number for that person. (two separate numbers). Should i use a nested switch case or an if else statement? or is there another way for doing this? maybe instead of making strings of each quote, make text files with the quotes(1 text file per person) n use a single switch case to get person n goto line no. of the text file for that person? which method would be faster n easier for the mobile it is being run on? Thanks in advance! :)
Switch, however that is not the best way. You should use a database, but if your not comforable with that write a program to create a file for each person and then list the quotes such as here for britneyspears.txt
Christina Aguilera and I are friends no matter what the media makes up.
Don't treat me like a little girl.
England is one of my favorite places. The fans are just so crazy.
Every night, I have to read a book, so that my mind will stop thinking about things that I stress about.
Have your program look up the files from a directory and list names, then the file could be loaded and split into strings. Using a database is preferable though and will be much much faster.
Neither.
Use a database, please! If you have numbers (id!) you can find that quote lightening quick on account of indexes.
You could load quote files on-demand and cache their quotes for use in the same session, e.g.:
public class Quotes {
Hashtable cache;
public Quotes() {
cache = new Hashtable();
}
public String getQuote(int person, int quoteNumber) {
if (!cache.containsKey(new Integer(person))) {
try {
loadQuoteFile(person);
} catch (IOException e) {
// Invalid person / file - do what you will
}
}
String[] quotes = (String[])cache.get(new Integer(person));
if (quoteNumber < 0) return quotes[0];
if (quoteNumber >= quotes.length) return quotes[quotes.length - 1];
return quotes[quoteNumber];
}
private void loadQuoteFile(int person) throws IOException {
String[] quotes;
// Load the file
// Parse into strings using linebreaks as the delimiter
cache.put(new Integer(person), quotes);
}
}
You'll likely need the text file approach anyway, unless you're keen on writing or generating code for a class containing all the quote data - e.g. a String[][]
, with person and quote number as indices and another String[] containing people's names - which is another option if you have enough memory available. Getting a particular quote from such a structure would just be a case using the given numbers asarray indices (performing range chaching as you go).
精彩评论