Quote:
Originally Posted by Fou-Lu
Depends on how fancy and efficient you want to be.
For a few words, the easiest way is to simply read all the lines into a collection such as an ArrayList<String> or Vector<String> using the BufferedReader's readLine method, and then execute a Collections.suffle on your list. Simply pull the first item out when complete.
For something more efficient if you were using hundreds or thousands of words, I'd use a random access file. It wastes more space overall since you space pad strings that are less than your maximum size, but you can jump right to the record in the file to read it, effectively giving you an O(1) access magnitude. I consider O(1) on a lookup > the waste of disk space. This works by allowing the file to contain two numbers at the head of the file, one representing the maximum length of the string, and another representing the number of records (using a byte and a long would work for this example). Then you use the seek method to move to the start of the record (record * size of record + 33 bytes), and read the next size of record. The random can occur between 1 and the number of records.
|
Okay, well i've done something like this for the array list:
try {
BufferedReader br= new BufferedReader(new FileReader("words.txt));
List<String> lines = new ArrayList<String>();
String line = br.readLine();
while(line !=null){
lines.add(line.replace(">", ""));
line = br.readLine();
}
not sure if it's right but if it is, how would I get a shuffle from after that?