Results 1 to 7 of 7
  1. #1
    parulmahajan is offline Member
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

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

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    What stops you from trying?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    parulmahajan is offline Member
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

    Default

    Illegeal modifier for parameter a; only final is permitted,

  4. #4
    parulmahajan is offline Member
    Join Date
    Jun 2010
    Posts
    5
    Rep Power
    0

    Default

    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.

  5. #5
    Ike
    Ike is offline Member
    Join Date
    Aug 2011
    Posts
    21
    Rep Power
    0

    Default

    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 ?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,416
    Rep Power
    20

    Default

    Ike, like Junky said, get your own thread. I'm closing this one and your post (and Junky's response) will be removed later.

    db

Similar Threads

  1. initialize a number, which is read in from a file, into an array
    By little_polarbear in forum New To Java
    Replies: 19
    Last Post: 06-10-2008, 03:53 AM
  2. How to initialize a two dimensional Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:48 PM
  3. How to initialize an Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:47 PM
  4. How to initialize array at runtime
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-09-2007, 03:47 PM
  5. Initialize array at runtime
    By javaplus in forum Java Tip
    Replies: 2
    Last Post: 11-09-2007, 11:44 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
  •