8 Replies Latest reply on Jun 2, 2008 9:35 AM by 807601

    Array expansion method not working?

    807601
      Hey, I've written an array expansion method for my Computer Science class (I'm a high school student) and it doesn't seem to work... it doesn't return an error, it tells me it's working, but it simply fails to function.

      The method code is:
      public static boolean expand ( int[] arr, int expander )
           {
                //Check for illegal expansion size
                if ( expander >= 0 )
                {
                     //Create a new array with an expanded size
                     int[] bigArr = new int[ ( arr.length + expander ) ];
                     //Fill the new array with the old array's contents
                     for ( int index = 0; index < arr.length; index++ )
                          bigArr[ index ] = arr[ index ];
                     //Set the passed array to the expanded array
                     arr = bigArr;
                     //Fucking thing won't work
                     System.out.println ( arr.length );
                     //Return true to indicate success
                     return true;
                }
                //Return false to indicate illegal size
                return false;
           }
      I call it in
      //Create the array holding the fill data
                int[] arrFill = { 15, -23, 47, 0, 78, 78, -5, 39, -23, 2 };
                //Expand arrFill
                ArrayTools.expand ( arrFill, 15 );
                //Set arr to arrFill
                arr = arrFill;
                // FUCKING THING SUCKS [/Oreilly]
                System.out.println ( arr.length );
                //Set the slot count
                slots = 10;
      So when I call fillArray, I get the output
      25
      10
      Meaning that expand sets the arr to 25 without problem, but somehow it gets ignored, because fillArray tells me that arr is still at 10.

      arr is oringially initialized to an array of integers with size 25:
      /*private*/ public int[] arr;
      InsDel ()
           {
                slots = 0;
                arr = new int[ 25 ];
           }
      slots is a variable keeping track of how many slots are being used, because my CS teacher won't allow me to just resize the array as necessary.

      I've tried setting expand to a void method, which wouldn't make any sense, but that didn't work either.
      I used to set arr equal to arrFill, then expand arr, but that wouldn't do anything either.

      I'd like to finish this sometime before 6:00 so I can turn it in on time, and I suppose i could just do it tediously by setting each array index manually, but I'fd like my expand method to be useable, considering I spent all of two minutes on it.
        • 1. Re: Array expansion method not working?
          752426
          The simple reason is there is no pass by reference in java. Nothing you do in expand() method will alter the arrFill array.
          ArrayTools.expand ( arrFill, 15 );
          Possible solution is: Make your method return the array and call it like this
          arrFill = ArrayTools.expand ( arrFill, 15 );
          • 2. Re: Array expansion method not working?
            807601
            No pass by reference? Elaborate. I don't think I'm passing or returning anything from the method, and if you can't pass references to methods, God sever my left testicle.

            I don't believe one can return arrays, correct me if I'm wrong.
            • 3. Re: Array expansion method not working?
              752426
              No pass by reference? Elaborate. I don't think I'm passing or returning anything from the method, and if you can't pass references to methods, God sever my left testicle.
              This topic has been dealt so many times in this forum. Please search the forum, you will find many of them
              I don't believe one can return arrays, correct me if I'm wrong.
              Arrays can be returned.
              • 4. Re: Array expansion method not working?
                807601
                Thanks, I'll search the forums.

                As to returning arrays, awesome. I always figured you should be able to, but was under the impression you couldn't.

                Great, I think I get the pass by reference vs. pass by value issue.

                Edited by: drshotgun on Jun 2, 2008 1:20 AM
                • 5. Re: Array expansion method not working?
                  807601
                  drshotgun wrote:
                  No pass by reference? [...] if you can't pass references to methods, God sever my left testicle.
                  Yes, you can pass references to methods... BY VALUE. That's why the line
                  arr = bigArr;
                  Only assigns a new reference to your formal parameter, which is a local variable not connected to the reference you passed in.

                  Passing a reference by value (this is what you do in Java) is not the same as passing objects by reference (which is impossible in Java for two reasons, because objects aren't values, and there is no pass by reference).
                  • 6. Re: Array expansion method not working?
                    807601
                    Yeah, I'm getting it now, although it seems kind of irritating. What I don't understand is why. It seems to me that pass by reference makes a lot more sense than pass by value.
                    • 7. Re: Array expansion method not working?
                      807601
                      drshotgun wrote:
                      It seems to me that pass by reference makes a lot more sense than pass by value.
                      Pass by reference can only work if what you pass in is a variable. What behavior would you expect from code like this?
                      public class Pass
                      {
                           public static void test1(int i)
                           {
                                i = 10;
                           }
                           
                           public static void test2(String s)
                           {
                                s = "hello world";
                           }
                           
                           public static void main(String[] args)
                           {
                                test1(5);
                                test2("goodbye");
                           }
                      }
                      Should 5 now be equal to 10? Should "goodbye" be the same as "hello world" from now on? ;-)
                      • 8. Re: Array expansion method not working?
                        807601
                        You got me there, although since neither 5 not "goodbye" are references, perhaps the compiler would understand to pass them by value. I suppose it's all or nothing, though.

                        Edited by: drshotgun on Jun 2, 2008 2:34 AM