Help with Java needed.
Results 1 to 4 of 4

Thread: Help with Java needed.

  1. #1
    Senior Member
    Join Date
    Jun 2003
    Posts
    772

    Help with Java needed.

    I want to create a program which dynamically creates multidimensional arrays, i.e. the program does not know how many dimensions the array will have as this is specified upon execution.

    Code:
    import java.lang.reflect.*;
    
    class Stuff {
    
       public static void main(String[] args) {
          int[] dimensions = new int[Integer.parseInt(args[0])];
          
          for (int i = 0; i < dimensions.length; i++) {
             dimensions[i] = 10;
          }
          int[][][][] stuff = (int[][][][])Array.newInstance(int.class, dimensions);
       
          stuff[9][8][9][9] = 8;      
       }
    }
    It is possible to create such arrays by using Array.newInstance(), this method returns an Object and it must be typecasted if being used as in the example.
    For example:
    Code:
    int[] dimensions = {10, 10, 10, 10};
    int[][][][] stuff = (int[][][][])Array.newInstance(int.class, dimensions);
    This will create a 4 dimensional array with 10 elements in each 'dimension'.

    Now the problem is that having to use the constructor int[][][][] defeats the point of that method, as one still has to know how many dimensions the array will have.
    Does anybody know how to avoid this?
    As stated, I want a program that creates an n-dimensional array, where n is specified by the user.
    The above sentences are produced by the propaganda and indoctrination of people manipulating my mind since 1987, hence, I cannot be held responsible for this post\'s content - me

    www.elhalf.com

  2. #2
    Ninja Code Monkey
    Join Date
    Nov 2001
    Location
    Washington State
    Posts
    1,027
    Why aren't you using a collection?

    http://java.sun.com/docs/books/tutor...ons/index.html
    "When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
    "There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
    "Mischief my ass, you are an unethical moron." - chsh
    Blog of X

  3. #3
    Senior Member
    Join Date
    Jun 2003
    Posts
    772
    Hm, I've never had the need to use any of those.
    They seem to be the solution, thanks.
    The above sentences are produced by the propaganda and indoctrination of people manipulating my mind since 1987, hence, I cannot be held responsible for this post\'s content - me

    www.elhalf.com

  4. #4
    Ninja Code Monkey
    Join Date
    Nov 2001
    Location
    Washington State
    Posts
    1,027
    Anytime. =)
    "When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
    "There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
    "Mischief my ass, you are an unethical moron." - chsh
    Blog of X

Posting Permissions

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