11 Replies Latest reply: Oct 16, 2007 2:03 PM by 796440 RSS

    Casting Array

    807605
      int [][] array= new int [3][3];
              
       Object o1 = array;        // line 1
       Object [] o2= array;      // line 2
       Object [][] o3= array;    // line 3
      I got compiler error at line 3
        • 1. Re: Casting Array
          807605
          So what are you trying to do?
          • 2. Re: Casting Array
            807605
            Two issues:
            Howcome its allowed at line 2 to cast 2D array to 1D array?

            and why error at line 3 when cast 2D array to 2D array of type Object?
            • 3. Re: Casting Array
              baftos
              Aleed wrote:
              Two issues:
              Howcome its allowed at line 2 to cast 2D array to 1D array?

              and why error at line 3 when cast 2D array to 2D array of type Object?
              A 2D array is a 1D array of 1D arrays, so it is a 1D array.
              As for line 3, consider the following code, which compiles:
              String [][] array= new String [3][3];
                      
               Object o1 = array;        // line 1
               Object [] o2= array;      // line 2
               Object [][] o3= array;    // line 3
              There is a huge difference between ints and Strings. Strings are Objects, ints are not.
              • 4. Re: Casting Array
                807605
                +"A 2D array is a 1D array of 1D arrays, so it is a 1D array."+

                Yes, but i m not assigning individual array element, its a 2D array of ints..
                -----------------------------------------------
                +"There is a huge difference between ints and Strings. Strings are Objects, ints are not."+

                As I guess All arrays are of type Object regardless of their declared type, thats why i was able to cast it to object of type Object implicitly at line 1.
                • 5. Re: Casting Array
                  807605
                  There's really no such thing as a 2D array in java. It bothers me that they allow that syntax because it's misleading.
                  int[][] array = new int[] { new int[] {1,2,3}, new int[]{1,2,3}, new int[]{1,2,3} };
                  Anyway, here's an equivalent thing to each thing you were trying to do, and why it worked:
                  int [][] array= new int [3][3];    
                   Object o1 = array;        // array is an object of type int[]
                   Object [] o2= array;      // each array[i] is of type int[], and therefore is an Object
                   Object [][] o3= array;    // each element of array[i][j] is of type int, and is not an Object.
                  • 6. Re: Casting Array
                    807605
                    Anyway, here's how I would use the same functionality, without a 2-D array:
                    Map<Point,Integer> integerMatrix = new HashMap<Point,Integer>();
                    integerMatrix.put(new Point(0,0), Integer.valueOf(5));
                    integerMatrix.get(new Point(0,0));  //returns Integer (5)
                    • 7. Re: Casting Array
                      796440
                      Aleed wrote:
                      +"A 2D array is a 1D array of 1D arrays, so it is a 1D array."+

                      Yes, but i m not assigning individual array element, its a 2D array of ints..
                      int [][] array= new int [3][3];
                              
                       Object o1 = array;        // line 1
                       Object [] o2= array;      // line 2
                       Object [][] o3= array;    // line 3
                      array is an array. That means it's an Object. So line 1 works.

                      array's element type is int[]. That means array's elements are Objects. That is, array[x] is an Object of class int[]. So array is an array of Objects. So line 2 works.

                      For line 3 to work, array[x][y] would have to be an Object. But it's not.
                      • 8. Re: Casting Array
                        807605
                        by the way, you arent "casting".
                        • 9. Re: Casting Array
                          796440
                          TuringPest wrote:
                          by the way, you arent "casting".
                          There's some evidence that there's an implicit upcast going on here. I remember looking into the JLS and not being completely sure. Not that it really matters--that's simply a matter for academic debate. :-)
                          • 10. Re: Casting Array
                            807605
                            Thanks jverd
                            • 11. Re: Casting Array
                              796440
                              You're welcome, though endasil's reply 5 provided the same explanation before mine, and his was more concise.