Link to home
Start Free TrialLog in
Avatar of The_Kingpin08
The_Kingpin08

asked on

Create an array of vectors

Hi everybody,

I'm posting here because I close my previous question (https://www.experts-exchange.com/questions/21400413/Java-class-inheritance.html) too quick (the answer helped me understanding my mistake, but now I have a logic problem).

I have a class named Dictionnary with a vectors containing a list of words: "protected Vector mots;"

I created a new class in a different file name "Cruciverbist" which is declared like this: "public class Cruciverbiste extends Dictionnaire"

Now what I want to do is:
- First to declare an array that can contains 24 vectors of words.
- Then, make a constructor that receive a filename of a file containing the list of words.
- Finaly, separate the dictionnary' words depending of their length, putting them in different vectors of the vectors' array.

* There should be a vector for each length of word (2 to 25 letters by word) containing every words of this length.


Here's how I started:

public class Cruciverbist extends Dictionnary
{
      // First point. How do you declare an array of vectors ? It needs to have 24 elements, but I don't know how to initialize it with these elements...  I need to
           declare a new vector and add empty items ?

      // Constructor.
      public Cruciverbist (String filename) throws IOException
      {
              // I'd like to create a new Dictionnary with the filename passed in parameter.
              // Then I'll have to read each words of the "words" vector and add them in the different vectors of the array.
      }
}


public class Dictionnary
{
      protected Vector words;

      // constructor
      public Dictionnary(String filename) throws IOException
      {
            readWords(filenamer);
            quickSort(words, 0, words.size() - 1);
      }

[...]


I would really appreciate if anyone could help me with this one. Thanks

Frank
       
ASKER CERTIFIED SOLUTION
Avatar of Peter Kwan
Peter Kwan
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of The_Kingpin08
The_Kingpin08

ASKER

Thanks a lot pkwan, that looks perfect.

I still have 2 questions:

- In my Dictionnary's class constructor, I have a method that automatically fills the vector "words" with all the words from the file. Knowing this, does I still need the
  getWords function ? Because I'd like to keep the dictionnary class as it is if I can. So while creating a new Dictionnary, I thought that a new "words" would be filled
  and ready to used in my new class's constructor... That would give something like this:

  // Cruciverbiste constructor
  public Cruciverbiste (String nom_fichier) throws IOException
  {
        // Initializing the vector's elements
        for (int i = 0; i < 24; i++) arrWords[i] = new Vector();
                  
        Dictionnary d = new Dictionnary (filename);
            
          // Here we consider that the vector "words" was initialized when we created a new instance of Dictionnary
        for (int i =0; i< words.size(); i++)
        {
              String word = (String) words.elementAt(i);
            arrWords[word.length()-2].add(word);
      }      
  }


- I'm having an error in my constructor's structure, and I think it's my fault since I never pratice inheritance besides in books...

  >javac Cruciverbist.java
  Cruciverbist.java:20: Dictionnary(java.lang.String) in Dictionnary cannot be applied to ()
      {
        ^
  1 error
  >Exit code: 1
 
  And here are the line numbers:
  [19]public Cruciverbiste (String nom_fichier) throws IOException
  [20]{
  [...]


Thanks for your time, I trully appreciate.
Frank
You may be not needing this. You can also delete the Dictionnary d = new Dictionnary (filename);
as well.

To solve your problem, you can call

super(filename);

at the beginning of your constructor of Cruciverbist class