Results 1 to 3 of 3
- 05-01-2012, 03:41 PM #1
Member
- Join Date
- May 2012
- Posts
- 5
- Rep Power
- 0
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.gif)
----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!
-AppelLast edited by Appel; 05-01-2012 at 09:27 PM.
- 05-01-2012, 03:44 PM #2
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!
- 05-01-2012, 09:31 PM #3
Member
- Join Date
- May 2012
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Parallel arrays and classes
By Bakaouji in forum New To JavaReplies: 3Last Post: 11-24-2011, 04:23 PM -
Trouble with arrays and classes
By CuddlyKittens11 in forum Advanced JavaReplies: 3Last Post: 04-25-2011, 12:42 AM -
Im a little confused on what classes are!
By Meta in forum New To JavaReplies: 4Last Post: 04-22-2010, 08:58 AM -
Got confused with extending classes.
By nethz13 in forum New To JavaReplies: 7Last Post: 04-19-2010, 12:19 AM -
methods, classes, arrays.. oh my!
By katalyst in forum New To JavaReplies: 30Last Post: 03-30-2009, 12:57 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks