Java storage - arraylist?

nelnel Member Posts: 2,859 ■□□□□□□□□□
Hi Guys,

Im a total newbie at java so excuse the crap terminology i'll probably use. Anyway, im working on a client/server task for a module im studying which is similar to a kind of inventory. basically the client has options on the interface and the server interface displays the inventory - its ID, description etc etc. Now so far the server inventory data has been statically defined in the server class. However i want to ammend it so i can add an additional option to the client interface to add new items to the inventory.

Would i be right in thinking an arraylist is the best thing to store the information input from the user?

im starting to get a little stumped because we've never had a java lecture or anything to do with it before but where going to be getting coursework on it! icon_eek.gif ...without having any lectures on the coding!!!

..no idea where that logic comes from icon_scratch.gif
Xbox Live: Bring It On

Bsc (hons) Network Computing - 1st Class
WIP: Msc advanced networking

Comments

  • JDMurrayJDMurray Admin Posts: 13,090 Admin
    An ArrayList is for storing related information that is all the same data type. For example, if you have the ages of 100 people you could store them in an integer ArrayList 100 elements in size.

    If you can't modify the original class storing the inventory information, you'll need to write a new class to extend the original class, such as:
    public class InventoryInfo {
         public int ID;
         public string Name;
         public string Description;
    };
    
    public class InventoryInfo2 extends InventoryInfo {
         public Date LastModified;
         public long PartNumber;
    };
    
    The InventoryInfo class contains the original information, and InventoryInfo2 contains everything in InventoryInfo plus the new fields.
  • nelnel Member Posts: 2,859 ■□□□□□□□□□
    ahh, so you could not store both strings and integers. So would a hashtable be more what im looking for? im trying to store the information input by the client which will include both int and strings.
    Xbox Live: Bring It On

    Bsc (hons) Network Computing - 1st Class
    WIP: Msc advanced networking
  • JDMurrayJDMurray Admin Posts: 13,090 Admin
    nel wrote:
    ahh, so you could not store both strings and integers. So would a hashtable be more what im looking for? im trying to store the information input by the client which will include both int and strings.
    No, you need a class for multiple, independent data fields. You can store multiple class objects with an ArrayList, Queue, Stack, etc.
    public class InventoryInfo {
         public int ID;
         public string Name;
         public string Description;
    
        InventoryInfo(id, name, desc)
        {
            ID = id;
            Name = name;
            Description = desc;
        }
    }; 
    
    ArrayList<InventoryInfo> invenArray = new ArrayList<InventoryInfo>();
    
    invenArray.add(new InventoryInfo("001", "Nuts", "Hardware"));
    invenArray.add(new InventoryInfo("002", "Bolts", "Hardware"));
    invenArray.add(new InventoryInfo("003", "Wrench", "Tool"));
    
  • nelnel Member Posts: 2,859 ■□□□□□□□□□
    Ah, Thanks for that JD.

    Sorry for being stupid!
    Xbox Live: Bring It On

    Bsc (hons) Network Computing - 1st Class
    WIP: Msc advanced networking
  • JDMurrayJDMurray Admin Posts: 13,090 Admin
    nel wrote:
    Sorry for being stupid!
    Oh, hey, you're not stupid at all. I've just got 25 years of programming experience ahead of you. ;)

    Besides, I'm not (yet) a Java programmer either. Everything you need also works the same for C#.
Sign In or Register to comment.