Results 1 to 6 of 6
  1. #1
    bluefloyd8 is offline Member
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Default 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.

    Java 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:
    Java Code:
    parsedInfo[sites] = new ParsedSiteInfo();

  2. #2
    bluefloyd8 is offline Member
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Default

    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{ )

    Java Code:
         private ParsedSiteInfo[] parsedInfo;
         private int sites = 0;
    
         private void createNewSite() {
        	 this.parsedInfo[this.sites] = new ParsedSiteInfo();
        	 this.sites++;
         }
    Still doesnt work :(

  3. #3
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default 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:
    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++;
         }
    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
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  4. #4
    praveena is offline Member
    Join Date
    Jan 2008
    Posts
    8
    Rep Power
    0

    Default

    is it necessary to use arrays in your code ...
    You can still use LinkedList

  5. #5
    XiaoXiao is offline Member
    Join Date
    Jan 2008
    Posts
    6
    Rep Power
    0

    Default

    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());
    }

  6. #6
    bluefloyd8 is offline Member
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Default

    Cool guys, thanks. Ill look into using the ArrayList. I needs to be dynamic because memory management is important.

Similar Threads

  1. Creating an array of nonprimitive objects
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:46 PM
  2. Replies: 1
    Last Post: 03-25-2008, 06:06 AM
  3. Array with objects
    By toby in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 10:50 AM
  4. Help with Objects!
    By Shorinhio in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 10:32 PM
  5. array of objects
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-02-2007, 06:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •