Reputation: 281
I want to pick an entry randomly from a large list (about a 1000 entries). What is the best way of keeping that large list in my app? Should I put it into a large string array? Will it slow down my program too much when tried to randomly pick a string? Thanks!
Upvotes: 0
Views: 192
Reputation: 234807
First, 1000 strings is not a particularly large array size. Whether this is a lot of memory depends on whether the strings are long.
As to picking one at random, the number of elements should not be a factor at all in terms of how long it will take to pick one. Just generate a random integer between 0 and 1000 (exclusive) and pick the one at that index.
Random random = new Math.Random(); // done once only
int randomIndex = random.nextInt(1000); // each time you need an index
Upvotes: 4
Reputation: 5572
1000 entries are not a large list if we talk about say integers and short strings, if its complex objects I really like OrmLite for database persistence.
Upvotes: 0