Results 1 to 4 of 4

Thread: N00b Question

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    Canada
    Posts
    40

    N00b Question

    This is my first week or two trying Java and I've got instructions on how to declare mutli-demensional array's but it doesn't say how to set value's to them.

    Say I wanted to input from a file into an array.

    What would the code be?? (to fill a mutlidemnsional array)

  2. #2
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332
    //for a 1D array
    [scope] datatype variableName [] = new datatype [x];

    //for a 2D array
    [scope] datatype variableName [] [] = new datatype [x] [y];

    /*and so on for more dimensions and where X & Y are int demensions;*/
    "There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein

    If you are programming in Java use www.NetBeans.org

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Ive never had the need so far to declare a mutli-demensional array for use with a file but as CaptainPinko pointed out it can be done as

    Code:
    type[]identifier = new type[]
    where type can either be a primative type or an object type.
    Same goes for a mutli-demensional or (two dimensional array)

    Code:
    type[][]identifier = new type[][]
    When declaring mutli-demensional arrays you must either specify row, or rows and cols. ie....... [4][] or [4][4]. Using [][4] would give you an error.

    Arrays are pretty easy to work with but the manner in which they can be declared and initialized can somtimes lead to confusion. For instance....
    Code:
    int[]x = {0,1,2,3,4};
    is the equivalent to
    Code:
    int[]x = new int[]{0,1,2,3,4};
    Anonymous arrays can also be created, Neither the name of the array nor the dimension of the array is specified.
    Code:
    new int[] {0,1,2,3,4}
    If you want to retrieve data from a file then your getting into I/O which consists of streams and such...

  4. #4
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332
    you can also delcared multi-demensionial arrays like that:
    for example a 2D array {{1,2,3,5}, {6,7,8,9,10}}; this illustrates an important concept of Java. THERE ARE NO MUTLIDIMENSIONAL arrays. The work around is that you can have an array of arrays of data, which works rather the same way w/ the bonus that you can have ragged arrays.
    "There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein

    If you are programming in Java use www.NetBeans.org

Posting Permissions

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



Click Here to Expand Forum to Full Width