I have provided a list of over 19,000 \"words\" I randomly created. They are not
ID: 3537443 • Letter: I
Question
I have provided a list of over 19,000 "words" I randomly created.
They are not real words with meaning in a given language. All the
words are in upper case.
In a driver class, read the words in from the file and place each word in an ArrayOrderedList. Then once all the words are in the list, print the contents of the list.
Time this process, from opening the file until the entire list has been printed.
(see the Calendar class). Print the number of milliseconds elapsed.
Then in a separate driver, create an array of 26 OrderedLinkedLists. The first element in this array will be a reference to a linked list that holds all the words that start with the letter 'A', the second element in the array will be a reference to a linked list that holds the words that start with the letter 'B' and so on.
Open the same input file, put each word in the correct place in the correct linked list and then print each linked list. Again, time this process from the opening of the file until the entire list of words has been printed.
/**
* This class shows how to get the first letter from
* a word to determine which array element its corresponding
* linked list is located.
*
* @author Lorrie Lehmann
* @version 2/01/2013
*/
public class TestAscii
{
public static void main(String[] args)
{
String word = "hello";
int subscript = ((word.toUpperCase()).charAt(0)) - 'A';
System.out.println("the word goes in slot : " + subscript);
}
}
The link to the wordlist is:
https://www.dropbox.com/s/cx6brze8m190vc3/wordfile.txt
Please I will give points for fully working code...
Explanation / Answer
Here is the first half Juan
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
/* In a driver class, read the words in from the file and place each word in an ArrayOrderedList.
Then once all the words are in the list, print the contents of the list.
Time this process, from opening the file until the entire list has been printed. (see the Calendar class).
Print the number of milliseconds elapsed. */
public class ArrayOrderedList {
public static void main(String[] args) throws FileNotFoundException {
// First of all starting the timer as said in the question
long beginOfTimer=System.currentTimeMillis();
// Taking a scanner and putting the file into it.
//The location can be changed. You can put the text file anywhere you wish to put it.
Scanner scanner = new Scanner(new File("C:\eclipse\wordfile.txt"));
// Taking an arrayList
ArrayList<String> arrayOrderedList = new ArrayList<String>();
// Reading the contents of the file and storing it into the list
while (scanner.hasNext()){
arrayOrderedList.add(scanner.next());
}
// Most important step. To close the scanner instance
scanner.close();
// sorting the list as discussed in the question
Collections.sort(arrayOrderedList);
// printing all the elements of the list
System.out.println(arrayOrderedList);
// Stoping the timer. Total time = end time - start time
long endOfTimer=System.currentTimeMillis() - beginOfTimer;
// printing the timer value
System.out.println("Total time taken in milliseconds is :"+endOfTimer);
}
}
2nd half
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Method2
{
public static void main(String[] args) throws FileNotFoundException {
// First of all starting the timer as said in the question
long beginOfTimer=System.currentTimeMillis();
// Taking a scanner and putting the file into it.
//The location can be changed. You can put the text file anywhere you wish to put it.
Scanner scanner = new Scanner(new File("C:\eclipse\wordfile.txt"));
// creating the array
List[] array = new List [26];
// for all the slots of array, creating a dynamic linklist
for (int i = 0; i < 26; i++) {
array[i] = new LinkedList();
}
// checking for the slot any particular word will fit into.
while (scanner.hasNext()){
String word = scanner.next();
int subscript = ((word.toUpperCase()).charAt(0)) - 'A';
//adding the word into that particular index.
array[subscript].add(word);
}
// sorting within particular slot.
for (int i = 0; i < 26 ; i++)
{
Collections.sort(array[i]);
}
// printing the array
for (int i = 0; i < 26 ; i++)
{
System.out.println(array[i]);
}
// Most important step. To close the scanner instance
scanner.close();
// Stoping the timer. Total time = end time - start time
long endOfTimer=System.currentTimeMillis() - beginOfTimer;
// printing the timer value
System.out.println("Total time taken in milliseconds is :"+endOfTimer);
}
}