Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-24-2007, 03:59 PM
zoe zoe is offline
Member
 
Join Date: Jul 2007
Posts: 40
zoe is on a distinguished road
Help with array of elements
I'm fairly new to Java. I'm currently making a text RPG which I'm trying to port from C++. In the Java code, I have a class called Item that contains all my item variables and methods. Then, I have another class that is the Game class which is the game itself. Here is where all the items, players, etc.. get initialized. Right now I have something like:

Item club = new Item("club", 1, "A club");

Now, in C++ I used a estructure to get an array of item. This made it easy, for example, to tell where items are, since I could just do a for loop and cycle through the entire item list by using item[i].location.

Is there any way to make an array of type Item (or any class in general) in Java?
Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-24-2007, 06:33 PM
Member
 
Join Date: Jul 2007
Location: England, Bath
Posts: 47
shanePreater is on a distinguished road
Is the array you are creating a fixed size? If so and if you know the size you can simply create an array of Item objects ala:
Code:
Item itemList = new Item[size]; itemList[0] = new Item("club", 1, "A Club"); ...
However if you don't know the array size or if it is likely to change then your probably better off with an array:
Code:
List<Item> itemList = new ArrayList<Item>(); itemList.add(new Item("club", 1, "A Club")); ...
Then when you wish to retrieve an item at a given index use:
Code:
Item chosenItem = arrayList.get(index);
The generics and collections stuff in java is very similar to the STL stuff in C++ so if you have used that then your laughing, if not, its not too hard to pick up.

If you are using a JDK prior to 1.5 then drop the <> sections and cast the item returned from the get method to an Item.

Hope that helps.
__________________
Shane Preater -
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Elements package BlitzA New To Java 0 12-28-2007 12:58 AM
reference to elements in array Igor New To Java 1 12-14-2007 12:56 PM
problems with asigning elements of an array to a constructor rednessc New To Java 1 12-14-2007 08:25 AM
deleting elements nalinda New To Java 2 12-06-2007 02:42 AM
JSP Scripting Elements JavaForums Java Blogs 0 08-06-2007 04:41 PM


All times are GMT +3. The time now is 11:50 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org