Results 1 to 7 of 7
  1. #1
    smtwtfs is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default Got problem on accessing public (global) array

    I had this problem and i tried to search but could not find similar problems. There is an array i want to declare globally, which in java would be a "public variable", if my understanding is correct.

    The size of this array is not determined when i define it. The size will be determined until the last element of the array is filled in a while loop.

    here's my code, Thank you all in advance.


    Java Code:
    public class Calc {
    
    	public static int chunk = 0;
    	public static int [] count;
    
    	public static void screen(){
    		String tmpwords;
    		StringTokenizer tokenizer;	
    		boolean stop = false;
    		try {
    			InputStream srcdata = new FileInputStream("20090506.xml");
    			BufferedReader in = new BufferedReader(new InputStreamReader(srcdata, "UTF8"));
    			
    			String tmp=in.readLine();	
    
    			while(!tmp.startsWith("</Do")){
    				while(!tmp.startsWith("</t")){
    					tokenizer = new StringTokenizer(tmp," ");
    					while(tokenizer.hasMoreTokens()){
    						tmpwords = tokenizer.nextToken();								
    		//############################################	
    //#### the following line is the problem #######
    //##############################################	
    						Calc.count[Calc.chunk]++;   
    					}
    					tmp=in.readLine();
    				}
    				Calc.chunk++;
    			}
    			in.close();
    		}
    		catch (Exception e) {
    			System.err.println("Read sourse file exception " + e.toString());
    		}		
    	}
    
    	public static void main(String[] argv) {
    		screen();
    	}
    }

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    What happens at the line you are pointing to? Errors? Exception? Something else?

    What is the problem?
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    In Java arrays have a size the moment they are initialised and they have that size forever.

    Are you aware that in that code you don't initialise count anywhere? If you try to initialise it you will run up against the problem that you don't know how big to make it. Consider using a collection like List<Integer>.

  4. #4
    smtwtfs is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    In Java arrays have a size the moment they are initialised and they have that size forever.

    Are you aware that in that code you don't initialise count anywhere? If you try to initialise it you will run up against the problem that you don't know how big to make it. Consider using a collection like List<Integer>.
    yes thanks. that is the problem. so i tried
    Java Code:
     public static int [] count = new int[500];
    it worked but since i don't know how big the array is, allocated 500 to it may not be a wise approach.

    Is there a way I can dynamically allocate an array. I remember in C++ there is something called linked list but is there an easy way in java?

  5. #5
    alexoffspring is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    I think that pbrockway2 already told you what is a linked List in java

    Consider using a collection like List<Integer>. Add to pbrockway2's Reputation

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Is there a way I can dynamically allocate an array. I remember in C++ there is something called linked list but is there an easy way in java?

    Yes - there's a similar thing in Java. That's why I suggested a list. The link I gave discusses how to use lists.

    If you do read that tutorial and decide to use a list (the section on lists is near the start) and get stuck, post what you've tried.
    Last edited by pbrockway2; 02-15-2011 at 09:44 AM.

  7. #7
    smtwtfs is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    Thank you!

    sorry indeed you posted it! just list<integer> is a bit strange to me so i did not relate it to linked list. I will try it out.

Similar Threads

  1. Replies: 12
    Last Post: 12-12-2010, 07:04 PM
  2. Access Public Global Variables in class
    By spatel14 in forum New To Java
    Replies: 5
    Last Post: 07-08-2010, 11:50 AM
  3. Access Public Global Variables in class
    By spatel14 in forum New To Java
    Replies: 1
    Last Post: 07-07-2010, 08:41 PM
  4. Replies: 4
    Last Post: 10-22-2009, 06:45 PM
  5. Replies: 8
    Last Post: 03-21-2009, 10:34 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
  •