Storing instances of a Sub Class in it's Super Class (Which is an array)
I'm trying to create a 'WordList' which contains any number of 'Word' instances.
my WordList class is just an array.
my Word class is an object which contains a String and a Boolean.
I thought I had the program figured out but I am just hitting walls at the moment... Can anyone please tell me if I'm going about this the wrong way or show me how the next step works?
Code :
import java.util.ArrayList;
public class ClassWordList{
//Here I'm struggling with how to initiate this class because I don't know how to make it hold my array...
String[] WSList;
ClassWordList(){
WSList;
}
//I understand that the above code won't run, but I'm not sure what to do with it at this point...
//-------------------subclass---------------------
public static class ClassWord{
static String stringWord;
Boolean booleanFound;
ClassWord(String w, boolean f){
stringWord = w;
booleanFound = false;
}
public static String getStringWord(String w) {
return (stringWord);
}
public static void setStringWord(String value) {
stringWord = value;
}
}
//------------------subclass end------------------
//Here I have a method of creating the array, but I don't know how it should tie in with the WordList and the Word...
ArrayList<ClassWord> objectWord = new ArrayList<ClassWord>();
}
Many thanks in advance for your help with my first post :)
Re: Storing instances of a Sub Class in it's Super Class (Which is an array)
Here you go. I cleaned it up a lot. This should work
Code :
package simon;
import java.util.ArrayList;
public class ClassWordList {
public static void main(String[] args) {
new ClassWordList();
}
// An array of the words to store in the Array list.
private String[] WSList = { "word", "word2" };
public ClassWordList() {
for (int i = 0; i < WSList.length; i++)
objectWord.add(new ClassWord(WSList[i]));
for (int i = 0; i < objectWord.size(); i++)
System.out.println(objectWord.get(i).getStringWord());
}
// The subclass.
public static class ClassWord {
private String stringWord;
ClassWord(String w) {
stringWord = w;
}
public String getStringWord() {
return stringWord;
}
public void setStringWord(String value) {
stringWord = value;
}
}
// The array list.
ArrayList<ClassWord> objectWord = new ArrayList<ClassWord>();
}
Re: Storing instances of a Sub Class in it's Super Class (Which is an array)
Thank you so much you really helped me here :) All I need to do now is figure out how not to make the same mistakes again! Thank you so much Endian!
Re: Storing instances of a Sub Class in it's Super Class (Which is an array)
Quote:
Originally Posted by Endian
Here you go. I cleaned it up a lot. This should work
Endian, please read the forums rules, and the following article: http://www.javaprogrammingforums.com...n-feeding.html
Re: Storing instances of a Sub Class in it's Super Class (Which is an array)
Some of us struggle with understanding abstract information, this example fits in nicely with what I'm doing right now, so I can now work it, or at least I will be able to in a couple of hours.
Reverse engineering something relevant to me is the only way I can efficiently learn things. Learning disabilities suck.
Re: Storing instances of a Sub Class in it's Super Class (Which is an array)
Quote:
Originally Posted by
Hiroto
Some of us struggle with understanding abstract information, this example fits in nicely with what I'm doing right now, so I can now work it, or at least I will be able to in a couple of hours.
Reverse engineering something relevant to me is the only way I can efficiently learn things. Learning disabilities suck.
And I applaud you for taking the code and using it as a learning experience. Unfortunately, more often than not handing over code such as what was done above not only denies someone of the necessary learning process of building code and problem solving - one of the most important concepts in programming, but the code is often taken without question leading to academic dishonesty, and many more problems down the road. The article discusses this, and this is why we do have rules against it. Once again though, I am glad to hear you are taking it as a starting point to learn