Results 1 to 3 of 3
  1. #1
    Appel is offline Member
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default Erg! A bit confused on arrays and classes

    For some reason, I seem to have issue with using arrays and classes, books will show examples of classes that use very similarly named functions within a class like:

    myaccount.display ...
    youraccount.display...

    And that's great, but this isn't how your going to code with 1000's of account in real life, but say you have 100 accounts that you want to read in all at once, well how does one approach this?

    I'm an experienced procedural programmer - C and Assembly type stuff. It's not the little technical stuff I'm having an issue with, but maybe understanding how much I should make into class, believe me, I see the value in OOP. Now I've gone through a couple of books, and for the most part, things make perfect sense...for the most part, but for the life of me this isn't covered in my books...or maybe I'm just not getting this bit.

    Here goes. I got a bunch of Ships and am keeping track of their Model Name, Capacity, Speed...etc. First off, I'm thinking OK, Ill create a class, lets call it ShipCatalog. So when I want to view the information on a particular ship, I can call the class function (or however I'm suppose to refer to it) it would look somrthign like

    Ship.ShowIt....

    Here's the question, say I got 100 ships, am I suppose to instantiate an array of that class..IE in the main:.

    int NumberOfShips = 100;
    ShipCatalog Ships[];
    Ships = new ShipCatalog[NumberOfShips--]

    So a call to the class would look like:

    Ship[1].ShowIt

    where I have multiple copies of the class Ship, and just call on that copy

    ----or----

    Am I suppose to instantiate one copy of the class ShipCatalog, and with is that class create an array of data, so main might look like:

    ShipCatalog Ships;

    and within ships have an array of 100, so a call to the class would look like:

    Ship.ShowIt(1);

    Where I'm going to tell that class which array number I want to see.

    ============================

    Either way I have tried seems to have issues with it, or at least the way I'm trying to use it, and that's telling me there's something wrong.

    Thanks in advance fpr any help with this!

    -Appel
    Last edited by Appel; 05-01-2012 at 09:27 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Erg! A bit confused on arrays and classes

    You create an array of Objects (like ShipCatalogs) exactly how you create an array of anything else. Recommended reading: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    You might also look into using a List.

    Also, it's a good idea to follow the standard naming conventions. Methods and variable names start with lower-case letters, classes and whatnot start with a capital letter, that kind of thing.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Appel is offline Member
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default Re: Erg! A bit confused on arrays and classes

    Alright thanks got it, and now working as expected. Initially it twisted my brain to create and array of objects, and not just an array of data. Just some things books don't cover.

Similar Threads

  1. Parallel arrays and classes
    By Bakaouji in forum New To Java
    Replies: 3
    Last Post: 11-24-2011, 04:23 PM
  2. Trouble with arrays and classes
    By CuddlyKittens11 in forum Advanced Java
    Replies: 3
    Last Post: 04-25-2011, 12:42 AM
  3. Im a little confused on what classes are!
    By Meta in forum New To Java
    Replies: 4
    Last Post: 04-22-2010, 08:58 AM
  4. Got confused with extending classes.
    By nethz13 in forum New To Java
    Replies: 7
    Last Post: 04-19-2010, 12:19 AM
  5. methods, classes, arrays.. oh my!
    By katalyst in forum New To Java
    Replies: 30
    Last Post: 03-30-2009, 12:57 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
  •