I have two arrays.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MrHuggykins
    New Member
    • Mar 2008
    • 27

    I have two arrays.

    And I need to take the elements from array1 and put them at the end of array2. array1 will need to be empty afterwards, so no copying. Ideas? Or is there somewhere I can go to read about it?
  • MrHuggykins
    New Member
    • Mar 2008
    • 27

    #2
    Bump
    Up
    My
    Post?

    Comment

    • blazedaces
      Contributor
      • May 2007
      • 284

      #3
      I don't understand what's the problem? You know how to put a value in an array right? Why don't you just put the values in array1 ... in the values of array2 (at the end or wherever you mean) using a loop of some kind?

      -blazed

      Comment

      • MrHuggykins
        New Member
        • Mar 2008
        • 27

        #4
        The problem is I'm writing a method and the test program has to be able to take the elements OUT of the first array and put them into another.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by MrHuggykins
          The problem is I'm writing a method and the test program has to be able to take the elements OUT of the first array and put them into another.
          If you want to pass an array to a method and expect that that array be changed after the method call then you can't do that. Java passes by value so a copy of the array will be created when you call the method and changes made in the method are done to that copy not to the original array.
          By taking values out of an array do you mean resetting the array values to default values?

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by r035198x
            If you want to pass an array to a method and expect that that array be changed after the method call then you can't do that. Java passes by value so a copy of the array will be created when you call the method and changes made in the method are done to that copy not to the original array.
            Yoohoo! Don't forget to take your coffee! ;-)

            kind regards,

            Jos

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by JosAH
              Yoohoo! Don't forget to take your coffee! ;-)

              kind regards,

              Jos
              And right on cue the coffee comes.

              @OP You can forget the crap I wrote about not being able to change the array's values. Arrays are pointers and passing a pointer to an array to a method allows you to modify that array (through the pointer).

              I think I'll right an article about this so my fingers don't keep forgetting it.

              Thanks Jos.

              Comment

              • MrHuggykins
                New Member
                • Mar 2008
                • 27

                #8
                Originally posted by r035198x
                And right on cue the coffee comes.

                @OP You can forget the crap I wrote about not being able to change the array's values. Arrays are pointers and passing a pointer to an array to a method allows you to modify that array (through the pointer).

                I think I'll right an article about this so my fingers don't keep forgetting it.

                Thanks Jos.
                Well see.. I guess you'd copy them to the new array and then remove them from the first array. How would you do that?

                Edit: Remember I need to write a test class, so this has to be in method-form.

                Comment

                • blazedaces
                  Contributor
                  • May 2007
                  • 284

                  #9
                  Originally posted by MrHuggykins
                  Well see.. I guess you'd copy them to the new array and then remove them from the first array. How would you do that?

                  Edit: Remember I need to write a test class, so this has to be in method-form.
                  Why don't you just fill the array with a value, like make all the numbers in an int array = 0 again?

                  -blazed

                  Comment

                  • MrHuggykins
                    New Member
                    • Mar 2008
                    • 27

                    #10
                    Look at the title in under my name.

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by MrHuggykins
                      And I need to take the elements from array1 and put them at the end of array2. array1 will need to be empty afterwards, so no copying. Ideas? Or is there somewhere I can go to read about it?
                      I think that you don't really understand what an array is; if I do this:

                      [code=java]
                      T[] array= new T[42];
                      [/code]

                      I end up with 42 adjacent slots and each slot can contain a T, whatever that T is;
                      if T is a primitive (int, double etc). the slots of the new array have all their bits set
                      to zero which effectively results in values 0 or 0.0 or 0L for the primitive type T.

                      If T is some type of object (a class or interface or enum) all the bits of all the slots
                      also have their bits set to zero which effectively results in the value 'null' which
                      means that none of the slots point to a type T, whatever T happens to be.

                      But whatever you do you always have those 42 slots until you forget about the
                      entire array altogether, i.e. you can't add slots nor can you remove slots from the
                      array. It is one chunk of 42 adjacent slots with index values 0, 1, 2 ... 41

                      Your remark about 'empty arrays' and 'no copying' is confusing at best and doesn't
                      make any sense at worst. The only thing you can do with those slots in an array
                      is change their value or copy their value to somewhere else.

                      kind regards,

                      Jos

                      Comment

                      • MrHuggykins
                        New Member
                        • Mar 2008
                        • 27

                        #12
                        Originally posted by JosAH
                        I think that you don't really understand what an array is; if I do this:

                        [code=java]
                        T[] array= new T[42];
                        [/code]

                        I end up with 42 adjacent slots and each slot can contain a T, whatever that T is;
                        if T is a primitive (int, double etc). the slots of the new array have all their bits set
                        to zero which effectively results in values 0 or 0.0 or 0L for the primitive type T.

                        If T is some type of object (a class or interface or enum) all the bits of all the slots
                        also have their bits set to zero which effectively results in the value 'null' which
                        means that none of the slots point to a type T, whatever T happens to be.

                        But whatever you do you always have those 42 slots until you forget about the
                        entire array altogether, i.e. you can't add slots nor can you remove slots from the
                        array. It is one chunk of 42 adjacent slots with index values 0, 1, 2 ... 41

                        Your remark about 'empty arrays' and 'no copying' is confusing at best and doesn't
                        make any sense at worst. The only thing you can do with those slots in an array
                        is change their value or copy their value to somewhere else.

                        kind regards,

                        Jos
                        So let me get this straight.. if you make that T[42] you're saying there's NO WAY to resize an array? Also, can anyone maybe give me some live help? I really need to get this done =(

                        Comment

                        • Laharl
                          Recognized Expert Contributor
                          • Sep 2007
                          • 849

                          #13
                          No, you can't directly resize an array. If you want to do that, use an ArrayList or Vector (Google/the API is your friend).

                          Comment

                          • blazedaces
                            Contributor
                            • May 2007
                            • 284

                            #14
                            Originally posted by MrHuggykins
                            So let me get this straight.. if you make that T[42] you're saying there's NO WAY to resize an array? Also, can anyone maybe give me some live help? I really need to get this done =(
                            Send me a PM and I'll try my best to help you out. I'll try my best to guide you through this if you would like...

                            I'll check periodically...

                            -blazed

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by MrHuggykins
                              So let me get this straight.. if you make that T[42] you're saying there's NO WAY to resize an array? Also, can anyone maybe give me some live help? I really need to get this done =(
                              Yep, arrays are kindof stupid; if you want an array of another size you have to
                              create it, haul the elements over from the first array and forget the first array.
                              btw, that's exactly how the ArrayList does it.

                              kind regards,

                              Jos

                              Comment

                              Working...