Results 1 to 6 of 6
Thread: Array of Objects
- 01-22-2008, 06:55 AM #1
Member
- Join Date
- Jan 2008
- Posts
- 4
- Rep Power
- 0
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.
I ran this code with some debug print statements and determine the program as a problem with this line:Java Code:private ParsedSiteInfo[] parsedInfo; private int sites = 0; private void createNewSite() parsedInfo[sites] = new ParsedSiteInfo(); sites++; }
Java Code:parsedInfo[sites] = new ParsedSiteInfo();
- 01-22-2008, 07:08 AM #2
Member
- Join Date
- Jan 2008
- Posts
- 4
- Rep Power
- 0
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{ )
Still doesnt work :(Java Code:private ParsedSiteInfo[] parsedInfo; private int sites = 0; private void createNewSite() { this.parsedInfo[this.sites] = new ParsedSiteInfo(); this.sites++; }
- 01-22-2008, 09:22 AM #3
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:
Remember to check this limit each time you try to add elements. See this tutorial on vectors and other dynamic data structures.Java Code:[B]public static final int structureSizeLimit = 10000;[/B] private ParsedSiteInfo[] parsedInfo[B] = new ParsedSiteInfo[structureSizeLimit];[/B] private int sites = 0; private void createNewSite() { this.parsedInfo[this.sites] = new ParsedSiteInfo(); this.sites++; }
Good luck. :DEyes dwelling into the past are blind to what lies in the future. Step carefully.
- 01-22-2008, 10:29 AM #4
Member
- Join Date
- Jan 2008
- Posts
- 8
- Rep Power
- 0
is it necessary to use arrays in your code ...
You can still use LinkedList
- 01-22-2008, 03:11 PM #5
Member
- Join Date
- Jan 2008
- Posts
- 6
- Rep Power
- 0
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());
}
- 01-22-2008, 06:27 PM #6
Member
- Join Date
- Jan 2008
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Creating an array of nonprimitive objects
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:46 PM -
Traversing through a stack of objects, and puttin them info in an array
By szimme101 in forum New To JavaReplies: 1Last Post: 03-25-2008, 05:06 AM -
Array with objects
By toby in forum New To JavaReplies: 1Last Post: 07-25-2007, 09:50 AM -
Help with Objects!
By Shorinhio in forum New To JavaReplies: 1Last Post: 07-10-2007, 09:32 PM -
array of objects
By Jack in forum New To JavaReplies: 2Last Post: 07-02-2007, 05:24 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks