Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Create an array of vectors

Posted on 2005-04-24
3
Medium Priority
?
2,415 Views
Last Modified: 2008-01-09
Hi everybody,

I'm posting here because I close my previous question (http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21400413.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
       
0
Comment
Question by:The_Kingpin08
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 2000 total points
ID: 13854049
First point:
private Vector[] words1= new Vector[24];

In your constructor of Cruciverbist, before you put things in:
for (int i = 0; i < 24; i++)
    words1[i] = new Vector();

Second, in your Dictionary class, you need to have a getter for words:
public Vector getWords() { return words; }

Third, in your Cruciverbist class, try to use getWords method to get the vector of words.
Loop for the words vector and check for the length of each element.

Dictionary d = new Dictionary(filename);
words = d.getWords();
for (int i =0; i< words.size(); i++) {
   String word = (String)words.elementAt(i);
   words1[word.length()-2].add(word);
}


0
 

Author Comment

by:The_Kingpin08
ID: 13854135
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
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 13854212
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question