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 7 of 7

Thread: Java Class (C++ Structure Like) Help

  1. #1
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Java Class (C++ Structure Like) Help

    Hi, I am creating a class that is like a (struct in c++),

    class MyStructTest
    {
        public String var1, var2, var3;
     
        public MyStructTest(String var1, var2, var3) {
            this.var1 = var1;
            this.var2 = var2;
            this.var3 = var3;
        }
    }

    then i make an arraylist of the type MyStruct
    public ArrayList<MyStructTest> myList = new ArrayList<MyStructTest>();

    i want to be able to add items to the arraylist by being able to choose which String of the struct like
    myList . element1 . var1 = "something"; and so on
    and then being able to access like myList element 1's var1.
    how would i add things to the arraylist and access each part of each element as such.

    Thanks




    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Class (C++ Structure Like) Help

    i want to be able to add items to the arraylist
    Read the API doc for the ArrayList class to see what methods it has for adding items to it.

    access like myList element 1's var1.
    Again see the ArrayList API doc for methods that access the contents of the array list.
    Once you get a class object reference from the ArrayList, you can use that reference to get to its contents like var1.

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

    Default Re: Java Class (C++ Structure Like) Help

    Lists are based upon index...if you wish to be able to grab an object based upon a value, perhaps a Map might be more appropriate?
    (see The Map Interface (The Java™ Tutorials > Collections > Interfaces) )

  4. #4
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java Class (C++ Structure Like) Help

    Hi, I figured out what i was doing wrong.

    I had to create an object of the type MyStructTest, and then set its attributes. and then add that to the arraylist.

    Thanks.

  5. #5
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java Class (C++ Structure Like) Help

    I have a different problem, with the arraylist.

    I call a method from another class and add an element to that arraylist, if i print out the contents of the arraylist from within the method that added something to it, the arraylist contents are shown.

    But if i later call a method that is meant to show the contents, it says the arraylist is empty

    I dont understand why?

    thanks

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Class (C++ Structure Like) Help

    Make sure that there is only one arraylist with a specific name. If you have two with the same name there will be confusion. You will add something to one and then later look in the other one and it will be empty.

  7. #7
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java Class (C++ Structure Like) Help

    Hi, thanks, what you said made me realise what I was doing wrong =).
    What i was doing wrong was that I was making a reference to the other class multiple times in each part.
    e.g Something somethingRef = new Something();

    I made only one reference at the top of the code and now it all works perfectly.

    thank you =)

Similar Threads

  1. Help with java decision structure program
    By kingsnans in forum Object Oriented Programming
    Replies: 4
    Last Post: November 5th, 2011, 10:54 AM
  2. Setting up Directory Structure in Java Applications
    By Jyotirmoy in forum Member Introductions
    Replies: 0
    Last Post: July 28th, 2011, 05:28 AM
  3. Run command line structure in Java
    By soheilz92 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: April 20th, 2011, 02:26 AM
  4. Replies: 3
    Last Post: April 16th, 2011, 01:38 PM