Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Storing instances of a Sub Class in it's Super Class (Which is an array)

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Location
    Oxforfd, UK
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post 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?

    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 :)


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Posts
    8
    My Mood
    Sleepy
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default 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
    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>();
    }
    Last edited by Endian; November 6th, 2011 at 06:53 AM.

  3. The Following 2 Users Say Thank You to Endian For This Useful Post:

    Hiroto (November 6th, 2011), inthu (December 9th, 2011)

  4. #3
    Junior Member
    Join Date
    Nov 2011
    Location
    Oxforfd, UK
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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!

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

  6. #5
    Junior Member
    Join Date
    Nov 2011
    Location
    Oxforfd, UK
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  7. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Storing instances of a Sub Class in it's Super Class (Which is an array)

    Quote Originally Posted by Hiroto View Post
    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

  8. The Following User Says Thank You to copeg For This Useful Post:

    Hiroto (November 6th, 2011)

Similar Threads

  1. storing class in mysql
    By junnam in forum JDBC & Databases
    Replies: 1
    Last Post: September 20th, 2011, 06:16 PM
  2. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  3. In a class create an array list of elements of another class, help!
    By LadyBelka in forum Collections and Generics
    Replies: 3
    Last Post: May 4th, 2011, 05:00 PM
  4. Multiple class instances ??? But how ???
    By dumb_terminal in forum Object Oriented Programming
    Replies: 6
    Last Post: December 2nd, 2010, 08:42 AM
  5. Replies: 0
    Last Post: December 1st, 2010, 06:10 AM

Tags for this Thread