Results 1 to 7 of 7
- 06-07-2010, 11:45 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 5
- Rep Power
- 0
How to Declare and Initialize static array
HI ,
Can anyone Please tell me how to create and initialize a static array of type int.
Actually i have a question like this...
Which two code fragments correctly create and initialize a static array
of int elements? (Choose two.)
A. static final int[] a = { 100,200 };
B. static final int[] a;
static { a=new int[2]; a[0]=100; a[1]=200; }
C. static final int[] a = new int[2] { 100,200 };
D. static final int[] a;
static void init() { a = new int[3]; a[0]=100; a[1]=200; }
Please answer...
- 06-07-2010, 12:16 PM #2
What stops you from trying?
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 06-07-2010, 12:21 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 5
- Rep Power
- 0
Illegeal modifier for parameter a; only final is permitted,
- 06-07-2010, 12:29 PM #4
Member
- Join Date
- Jun 2010
- Posts
- 5
- Rep Power
- 0
hey thanks dear
it works.. problem was with the location of the array.
to create static array we must create and initialize it in static block only.
- 08-05-2011, 06:19 AM #5
Member
- Join Date
- Aug 2011
- Posts
- 21
- Rep Power
- 0
I'm working with some iteration pattern, and I don't know how many members array is going to have.
When I declare an array like:
static double[] data;
...I get errors. But if I do it like this, the problem is gone :
static double[] data = new double[90];
Values are added later with a method:
public void setData(int matrixSize){
for (int i=0 ; i<matrixSize ; i++){
data[i]=laGrange.output[i];
}
I even try doing:
...
static double[] data;
...
public void setData(int matrixSize){
data = new double[matrixSize];
for (int i=0 ; i<matrixSize ; i++){
data[i]=laGrange.output[i];
}
1. There is an error cause of my code(somewhere later) ?
2. There is no way to have an 'undefined size' static (or non-static) array ?
- 08-05-2011, 06:48 AM #6
In future start your own thread, do not hijack someone elses. Especially an old one.
If you do not know what size your array will be then use a data structure from the Collection framework. Probably an ArrayList.
- 08-05-2011, 07:48 AM #7
Similar Threads
-
initialize a number, which is read in from a file, into an array
By little_polarbear in forum New To JavaReplies: 19Last Post: 06-10-2008, 03:53 AM -
How to initialize a two dimensional Array
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:48 PM -
How to initialize an Array
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:47 PM -
How to initialize array at runtime
By Java Tip in forum Java TipReplies: 0Last Post: 11-09-2007, 03:47 PM -
Initialize array at runtime
By javaplus in forum Java TipReplies: 2Last Post: 11-09-2007, 11:44 AM


LinkBack URL
About LinkBacks

Bookmarks