Results 1 to 7 of 7
  1. #1
    TheGame is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Unhappy How to Store ArrayList into an Object?

    Hi, for part of an assignment, i need to store instances of a class into another class object using "an appropriate data structure"..

    For example, if I have an abstract class called ByteCode and i built an instance of the class HaltCode that extends ByteCode, and I want to store this instance into a class object like "Program program", the assignment tells me to store these instances in a data structure.

    So i chose an ArrayList<ByteCode> and stored the instances in it, and I returned this ArrayList into the object.

    e.g.
    Java Code:
    Program program = bcl.loadCodes();
    loadCodes() returns the ArrayList<ByteCode>

    my problem is unless I make the Program class extends ArrayList, i will get an incompatible type error..and my assignment specifically stated that the Program class only extends Object..

    I also cannot create an ArrayList instance in Program class like
    Java Code:
    program.arrayListByteCodes = bcl.loadCodes();
    because my assignment specifically stated that a line of code will include
    Java Code:
    Program program = bcl.loadCodes();
    So how do i do this? Thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    TheGame is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    well, i can't really post the assignment, since i'm only supposed to do a part of it and it is scattered around several pages..but here is the relevant parts:

    Java Code:
    ByteCodeLoader.java
    This is the class that will be in charge of loading the code from the source file. It also has a method loadCodes() that loads bytecode objects into an instance of the program class. 
    
    Program.java
    Holds the bytecode objects
    
    ByteCodeLoader uses Java reflection to create an actual class instance. 
    
    The Program class will hold the ByteCode instances that were created
    by ByteCodeLoader. There will be a line "Program program = bcl.loadCodes()"
    
    loadCodes() load codes from file, and returns the bytecode program in an appropriate data structure for processing by the VirtualMachine.

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default

    Hello TheGame, Welcome to the forums.

    I find your post very confusing. If you are attempting to declare an ArrayList, convert it to an Object, then convert it to an instance of Progam then it is not going to work.

    If you posted correctly and the code...

    Java Code:
    Program program = bcl.loadCodes();

    is to be used, then the the only way you are going to acheive this is if loadCodes() returns an instance of Program.

    I think you are on the right lines in terms of using the ArrayList but I think you might have misunderstood the requirements. Can you please explain exactly what is required or if possible the assignment brief.

    Regards.

  5. #5
    TheGame is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    yes, this is very confusing for me too..
    but the assignment specifically stated that

    "the instance of ByteCode will be stored in the Program object" and that
    loadCodes() will
    "return the bytecode program in an appropriate data structure",
    and i don't know how to do that if it should return an instance of Program..

  6. #6
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default

    Ignore my last, I didn't see the responses.

    Regards.

  7. #7
    TheGame is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    ok, i think i came up with something. can i use something like:
    Java Code:
    pseudocode:
    
    public static Program loadCodes()
    Program program;
    program.arraylist = ArrayList;   //i make Program have an arraylist instance
    return program;
    edit: yeah..this seems to work now.. is there any other way i can do this?
    Last edited by TheGame; 10-10-2010 at 11:12 PM.

Similar Threads

  1. Replies: 3
    Last Post: 03-15-2010, 08:44 PM
  2. How can i store ArrayList objects in Access database
    By frankycool in forum Advanced Java
    Replies: 3
    Last Post: 11-04-2009, 06:55 AM
  3. Replies: 3
    Last Post: 03-28-2009, 05:02 AM
  4. Replies: 1
    Last Post: 03-04-2009, 06:14 PM
  5. how to store an ArrayList<string> into cookies?
    By perplexingtrax in forum Java Servlet
    Replies: 7
    Last Post: 01-12-2009, 12:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •