Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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 01-22-2008, 08:55 AM
Member
 
Join Date: Jan 2008
Posts: 4
bluefloyd8 is on a distinguished road
Array of Objects
Hey All!

I need to make an array of objects ( of a class i created). I

Below is some of the code. I want to make an array, parsedInfo, of objects from the class ParsedSiteInfo. In a function not shown, I have a for-loop that calls the function createNewSite(). So the array needs to be able to grow dynamically.

Code:
private ParsedSiteInfo[] parsedInfo; private int sites = 0; private void createNewSite() parsedInfo[sites] = new ParsedSiteInfo(); sites++; }
I ran this code with some debug print statements and determine the program as a problem with this line:
Code:
parsedInfo[sites] = new ParsedSiteInfo();
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-22-2008, 09:08 AM
Member
 
Join Date: Jan 2008
Posts: 4
bluefloyd8 is on a distinguished road
I just tried the following code as a change. I wasnt putting "this" in front of variables. My code shown is part of a class ( public class ExampleHandler extends DefaultHandler{ )

Code:
private ParsedSiteInfo[] parsedInfo; private int sites = 0; private void createNewSite() { this.parsedInfo[this.sites] = new ParsedSiteInfo(); this.sites++; }
Still doesnt work
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-22-2008, 11:22 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Dynamic data structures
Hello bluefloyd8

Arrays are not dynamic data structures and you cannot make them (that easily). You need to specify their size before you use them. Your compiler probably gave you a null pointer exception, right? So change you code to:
Code:
public static final int structureSizeLimit = 10000; private ParsedSiteInfo[] parsedInfo = new ParsedSiteInfo[structureSizeLimit]; private int sites = 0; private void createNewSite() { this.parsedInfo[this.sites] = new ParsedSiteInfo(); this.sites++; }
Remember to check this limit each time you try to add elements. See this tutorial on vectors and other dynamic data structures.

Good luck.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-22-2008, 12:29 PM
Member
 
Join Date: Jan 2008
Posts: 8
praveena is on a distinguished road
is it necessary to use arrays in your code ...
You can still use LinkedList
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-22-2008, 05:11 PM
Member
 
Join Date: Jan 2008
Posts: 6
XiaoXiao is on a distinguished road
Hello bluefloyd8

Array is not a dynamic data structures.
Instead of using an Array ... u can use ArrayList which is dynamic .. u dont have to worry about the size increment .. tht will be taken care.
This should work for you .......


ArrayList<ParsedSiteInfo> parsedInfo = new ArrayList<ParsedSiteInfo>();
private void createNewSite(){
this.parsedInfo.add(new ParsedSiteInfo());
}
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-22-2008, 08:27 PM
Member
 
Join Date: Jan 2008
Posts: 4
bluefloyd8 is on a distinguished road
Cool guys, thanks. Ill look into using the ArrayList. I needs to be dynamic because memory management is important.
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
Creating an array of nonprimitive objects Java Tip java.lang 0 04-14-2008 10:46 PM
Traversing through a stack of objects, and puttin them info in an array szimme101 New To Java 1 03-25-2008 07:06 AM
Array with objects toby New To Java 1 07-25-2007 11:50 AM
Help with Objects! Shorinhio New To Java 1 07-10-2007 11:32 PM
array of objects Jack New To Java 2 07-02-2007 07:24 AM


All times are GMT +3. The time now is 01:34 PM.


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