-
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();
-
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 :(
-
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:
[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++;
}
Remember to check this limit each time you try to add elements. See this tutorial on vectors and other dynamic data structures.
Good luck. :D
-
is it necessary to use arrays in your code ...
You can still use LinkedList
-
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());
}
-
Cool guys, thanks. Ill look into using the ArrayList. I needs to be dynamic because memory management is important.