Using arrays of objects ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TonyB

    Using arrays of objects ?

    I'm new to java, and would like some advice on how to keep track of groups
    of objects where the number of objects is not predefined.
    For example say I have a toolbox class which contains an arbitrary number of
    tool objects. I want to have a method to add a tool to the toolbox object,
    and other methods to list the current contents of the toolbox for example.
    I could keep a reference to each tool object as added in an array of type
    tool but an array is declared with a fixed length, so short of defining the
    array to have a some arbitrary large size I can't see how to deal with this
    list of objects. I don't want to use a database at this time.
    Is there a better way to do this ?
    Tony


  • Casey Hawthorne

    #2
    Re: Using arrays of objects ?

    Read up on collection classes and post to comp.lang.java. help
    --
    Regards,
    Casey

    Comment

    • Mark Rafn

      #3
      Re: Using arrays of objects ?

      TonyB <tonyb@kerriswa y.freeserve.co. ukwrote:
      >I'm new to java, and would like some advice on how to keep track of groups
      >of objects where the number of objects is not predefined.
      java.util.Colle ction and friends make this pretty easy.
      >I could keep a reference to each tool object as added in an array of type
      >tool but an array is declared with a fixed length, so short of defining the
      >array to have a some arbitrary large size I can't see how to deal with this
      >list of objects. I don't want to use a database at this time.
      ArrayList<Tooli s the most common way to do this. Depending on your needs,
      you may prefer a HashSet, TreeSet, LinkedList, or some other Collection that
      fits your performance and usage requirements.
      --
      Mark Rafn dagon@dagon.net <http://www.dagon.net/>

      Comment

      • Joanna

        #4
        Re: Using arrays of objects ?


        "TonyB" <tonyb@kerriswa y.freeserve.co. ukwrote in message
        news:E9JDh.8799 6$tQ1.64679@fe0 4.news.easynews .com...
        I'm new to java, and would like some advice on how to keep track of groups
        of objects where the number of objects is not predefined.
        For example say I have a toolbox class which contains an arbitrary number
        of
        tool objects. I want to have a method to add a tool to the toolbox object,
        and other methods to list the current contents of the toolbox for example.
        I could keep a reference to each tool object as added in an array of type
        tool but an array is declared with a fixed length, so short of defining
        the
        array to have a some arbitrary large size I can't see how to deal with
        this
        list of objects. I don't want to use a database at this time.
        Is there a better way to do this ?
        Tony
        here we go


        Joanna


        Comment

        • j1mb0jay

          #5
          Re: Using arrays of objects ?

          TonyB wrote:
          I'm new to java, and would like some advice on how to keep track of
          groups of objects where the number of objects is not predefined.
          For example say I have a toolbox class which contains an arbitrary
          number of tool objects. I want to have a method to add a tool to the
          toolbox object, and other methods to list the current contents of the
          toolbox for example. I could keep a reference to each tool object as
          added in an array of type tool but an array is declared with a fixed
          length, so short of defining the array to have a some arbitrary large
          size I can't see how to deal with this list of objects. I don't want
          to use a database at this time. Is there a better way to do this ?
          Tony
          ArrayLists would seem to the answer for what you are looking for.



          There is a Iterator class for going through each of the objects in a array,
          try not to store objects in an array, try to type them first (looking into
          Interfaces).




          --
          Regards JJ (UWA)

          Comment

          • TideRider

            #6
            Re: Using arrays of objects ?

            I would probably use an ArrayList (from java.util). I would also define either an
            interface or base class for Tool, from which all of the tools are derived, and
            (using Java 5 or higher) parameterize the ArrayList:

            ArrayList<Toolt oolbox = new ArrayList<Tool> ();

            To operate on Tool in the toolbox (again, with Java 5 or higher), you can use:

            for (Tool tool : toolbox)
            {
            //tool variable contains the current tool
            }


            --
            TideRider


            "TonyB" <tonyb@kerriswa y.freeserve.co. ukwrote in message news:E9JDh.8799 6$tQ1.64679@fe0 4.news.easynews .com...
            | I'm new to java, and would like some advice on how to keep track of groups
            | of objects where the number of objects is not predefined.
            | For example say I have a toolbox class which contains an arbitrary number of
            | tool objects. I want to have a method to add a tool to the toolbox object,
            | and other methods to list the current contents of the toolbox for example.
            | I could keep a reference to each tool object as added in an array of type
            | tool but an array is declared with a fixed length, so short of defining the
            | array to have a some arbitrary large size I can't see how to deal with this
            | list of objects. I don't want to use a database at this time.
            | Is there a better way to do this ?
            | Tony
            |
            |


            Comment

            • Lew

              #7
              Re: Using arrays of objects ?

              TideRider wrote:
              I would probably use ...
              Just so you know, many newsreaders, mine included, clip material that follows
              your sig in the reply, and display it "dimmed" or gray in the initial reading.
              This happened with your replies, which were top-posted.

              Top-posting also disrupts the flow of the message and makes it harder to read.

              Please don't top-post.

              As to your suggestion to use
              ArrayList<Toolt oolbox = new ArrayList<Tool> ();
              It'd likely be better to use

              List<Tooltoolbo x = ...

              or even

              Collection<Tool toolbox = ...

              For example, if a given Tool should appear only once in 'toolbox', then the
              implementing class might better be a Set<Tool>.

              --
              Lew

              Comment

              Working...