2 Replies Latest reply: Apr 1, 2008 7:43 AM by 807601 RSS

    Array containing array, and multidimensional array?

    807601
      Is it possible to create two arrays, where one array contains boolean and the other contain arrays?
      I would think it would be something like this

      boolean array[];
      array newArray[];


      The other question is how to use multidimensional array.
      I know the declaration

      int array[][];

      but how do i add data to the array? I have tried this
      array[0][1] = {1,3};

      but it dosent work.
        • 1. Re: Array containing array, and multidimensional array?
          800308
          All is explained meticulously here... http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html

          Edited by: corlettk on 1/04/2008 12:41 - it doesn't cover doubly subscribted arrays - here's an example - http://www.java.happycodings.com/Core_Java/code26.html
          • 2. Re: Array containing array, and multidimensional array?
            807601
            Yes, it wouldn't work, since you have a 2 dimensional boolean array, what you have done is the same as:
            boolean[][] myArray = new boolean[n][m];
            //you can address it by
            myArray[0][1] = true;
            if you want to have say a boolean and an int together, then you need make an array of your class that implements your data structure:
            class myData{
              boolean b = false;
              int          i  = 0;
            }
            //and then use the declaration:
            myData[][] data = new myData[n][m];
            data[0][1] = new myData; //you've created an array, but not an element of the array until here.
            data[0][1].b = true;
            data[0][1].i  = 128;