Results 1 to 7 of 7
- 02-15-2011, 06:44 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
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(); } }
- 02-15-2011, 07:02 AM #2
What happens at the line you are pointing to? Errors? Exception? Something else?
What is the problem?Java Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 02-15-2011, 07:05 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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>.
- 02-15-2011, 08:27 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
yes thanks. that is the problem. so i tried
it worked but since i don't know how big the array is, allocated 500 to it may not be a wise approach.Java Code:public static int [] count = new int[500];
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?
- 02-15-2011, 08:41 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
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
- 02-15-2011, 08:42 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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 08:44 AM.
- 02-15-2011, 09:02 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Making an array public or any variable to use across classes and Java files?
By Jonatan10 in forum New To JavaReplies: 12Last Post: 12-12-2010, 06:04 PM -
Access Public Global Variables in class
By spatel14 in forum New To JavaReplies: 5Last Post: 07-08-2010, 10:50 AM -
Access Public Global Variables in class
By spatel14 in forum New To JavaReplies: 1Last Post: 07-07-2010, 07:41 PM -
Accessing non-static public variables from another class
By ribbs2521 in forum New To JavaReplies: 4Last Post: 10-22-2009, 05:45 PM -
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks