Append one array to another array

Discussion in 'Java' started by June Moore, Oct 28, 2004.

  1. June Moore

    June Moore Guest

    Hi,
    I would like to append the content of an array (Student[]) to another
    array (Student[]).

    Student[] oldStudent
    Student[] newStudent

    Can I do this?
    newStudent = newStudent.add(oldStudent);

    Thanks,
    June.....
    June Moore, Oct 28, 2004
    #1
    1. Advertising

  2. June Moore

    VisionSet Guest

    "June Moore" <> wrote in message
    news:...
    > Hi,
    > I would like to append the content of an array (Student[]) to another
    > array (Student[]).
    >
    > Student[] oldStudent
    > Student[] newStudent
    >
    > Can I do this?
    > newStudent = newStudent.add(oldStudent);


    No, because an array is immutable.
    You will have to create a new array and copy the contents of both into it.
    You may find System.arrayCopy() useful.

    --
    Mike W
    VisionSet, Oct 28, 2004
    #2
    1. Advertising

  3. June Moore

    VisionSet Guest

    "VisionSet" <> wrote in message
    news:EX3gd.62$...
    >
    > "June Moore" <> wrote in message
    > news:...
    > > Hi,
    > > I would like to append the content of an array (Student[]) to another
    > > array (Student[]).
    > >
    > > Student[] oldStudent
    > > Student[] newStudent
    > >
    > > Can I do this?
    > > newStudent = newStudent.add(oldStudent);

    >
    > No, because an array is immutable.
    > You will have to create a new array and copy the contents of both into it.
    > You may find System.arrayCopy() useful.


    When I say immutable I refer to the size, this being your problem.
    It is a poor word choice since an array is mutable as far as changing the
    value of the element references.

    ie you can do this

    String[] myString = {"Original"};

    myStringArray[0] = "Mutated";


    Also note you can not call methods on Arrays, although they are Objects
    there is no API for them.

    java.util.Arrays being a utility class for services on Arrays and
    java.lang.reflect.Array being a introspection class for accessing an Array
    object. Neither can produce objects.

    --
    Mike W
    VisionSet, Oct 28, 2004
    #3
  4. > Student[] oldStudent
    > Student[] newStudent
    >
    > Can I do this?
    > newStudent = newStudent.add(oldStudent);


    You can do something similar, if you use Vectors instead of arrays.

    Niels Dybdahl
    Niels Dybdahl, Oct 28, 2004
    #4
  5. (June Moore) writes:

    > I would like to append the content of an array (Student[]) to another
    > array (Student[]).
    >
    > Student[] oldStudent
    > Student[] newStudent
    >
    > Can I do this?
    > newStudent = newStudent.add(oldStudent);


    No, as the length of an array cannot change after it is created.

    You need to create a new array to hold the data:
    ---
    Student[] temp = new Student[oldStudent.length + newStudent.length];
    System.arraycopy(newStudent, 0, temp, 0, newStudent.length)
    System.arraycopy(oldStudent, 0, temp, newStudent.length, oldStudent.length);
    newStudent = temp;
    ---

    A little less low level (no fiddling with indices), you can use
    collections to combine the two arrays:
    ---
    List<Student> temp =
    new ArrayList<Student>(oldStudent.length + newStudent.length);
    temp.add(Arrays.asList(newStudent));
    temp.add(Arrays.asList(oldStudent));
    newStudent = (Student[]) temp.toArray(new Student[temp.size()]);
    ---

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Oct 28, 2004
    #5
  6. June Moore

    Oscar kind Guest

    Niels Dybdahl <-graphics.com> wrote:
    >
    > You can do something similar, if you use Vectors instead of arrays.


    Or better yet, use a List (and a class that implements this interface).
    Since the new Collections framework came out, I consider it a good idea
    not to use a Vector, unless:
    - I know the subtle differences between a Vector and a List
    - A List doesn't suffice, and I need the specifics of a Vector


    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
    Oscar kind, Oct 28, 2004
    #6
  7. June Moore

    Tony Morris Guest


    > Also note you can not call methods on Arrays, although they are Objects
    > there is no API for them.


    This is untrue.

    Here is an example:

    class X
    {
    public static void main(String[] args)
    {
    int[] i = new int[]{6, 7, 8};

    System.out.println(i.toString());
    System.out.println(i.hashCode());
    System.out.println(i.equals(null));
    }
    }
    Tony Morris, Oct 29, 2004
    #7
  8. June Moore

    VisionSet Guest

    "Tony Morris" <> wrote in message
    news:klogd.2759$...
    >
    > > Also note you can not call methods on Arrays, although they are Objects
    > > there is no API for them.

    >
    > This is untrue.
    >
    > Here is an example:
    >
    > class X
    > {
    > public static void main(String[] args)
    > {
    > int[] i = new int[]{6, 7, 8};
    >
    > System.out.println(i.toString());
    > System.out.println(i.hashCode());
    > System.out.println(i.equals(null));
    > }
    > }


    Of course, doh! Everything is an Object and inherits from Object (if it
    isn't a primitive).
    I suppose I meant it doesn't have its *OWN* public API.

    --
    Mike W
    VisionSet, Oct 29, 2004
    #8
  9. VisionSet wrote:
    >>>Also note you can not call methods on Arrays, although they are Objects
    >>>there is no API for them.

    >>
    >>This is untrue.


    []

    > Of course, doh! Everything is an Object and inherits from Object (if it
    > isn't a primitive).
    > I suppose I meant it doesn't have its *OWN* public API.


    Actually, it has. Unlike Object instances, arrays have the public length field
    and a public clone() method. See Java Language Specification section 10.7
    Michael Borgwardt, Oct 29, 2004
    #9
  10. June Moore

    VisionSet Guest

    "Michael Borgwardt" <> wrote in message
    news:...

    > > I suppose I meant it doesn't have its *OWN* public API.

    >
    > Actually, it has. Unlike Object instances, arrays have the public length

    field
    > and a public clone() method. See Java Language Specification section 10.7


    I wonder why they didn't make it a length() method - would feel more
    intuitive especially since you stumble across this very early in learning
    the language.

    How could they have included the Array type in the javadocs?

    --
    Mike W
    VisionSet, Oct 29, 2004
    #10
  11. VisionSet wrote:
    >>Actually, it has. Unlike Object instances, arrays have the public length field
    >>and a public clone() method. See Java Language Specification section 10.7

    >
    > I wonder why they didn't make it a length() method - would feel more
    > intuitive especially since you stumble across this very early in learning
    > the language.


    I suppose it was for performance reasons - at the time the spec was written,
    method calls really were much slower than access to a final field.

    > How could they have included the Array type in the javadocs?


    There's no single "Array type". Of course, all Array types are derived from
    Object[], but none of them, including Object[] are real classes, they're
    synthesized by the JVM. And you can't have a javadoc entry for each of them,
    because there's an infinite number: consider Object[], Object[][],
    Object[][][], etc...
    Michael Borgwardt, Oct 29, 2004
    #11
  12. June Moore

    VisionSet Guest

    "Michael Borgwardt" <> wrote in message
    news:...

    >
    > > How could they have included the Array type in the javadocs?

    >
    > There's no single "Array type". Of course, all Array types are derived

    from
    > Object[], but none of them, including Object[] are real classes, they're
    > synthesized by the JVM. And you can't have a javadoc entry for each of

    them,
    > because there's an infinite number: consider Object[], Object[][],
    > Object[][][], etc...


    Makes sense. Thanks Michael.

    --
    Mike W
    VisionSet, Oct 29, 2004
    #12
  13. Michael Borgwardt wrote:
    >
    > VisionSet wrote:
    >
    > > How could they have included the Array type in the javadocs?

    >
    > There's no single "Array type". Of course, all Array types are derived from
    > Object[], but none of them, including Object[] are real classes, they're
    > synthesized by the JVM.


    That's not right. An array of primitives can't be derived from Object[].

    --
    Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
    ==============================================================
    * The Ultimate DBMS is here!
    * FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
    Lee Fesperman, Oct 29, 2004
    #13
  14. Lee Fesperman wrote:
    >>There's no single "Array type". Of course, all Array types are derived from
    >>Object[], but none of them, including Object[] are real classes, they're
    >>synthesized by the JVM.

    >
    >
    > That's not right. An array of primitives can't be derived from Object[].


    Ah, you're right of course, I forgot about those.

    So there's not even a common superclass for all array types (except for Object).
    Michael Borgwardt, Oct 29, 2004
    #14
  15. Michael Borgwardt wrote:
    >
    > Lee Fesperman wrote:
    > >>There's no single "Array type". Of course, all Array types are derived from
    > >>Object[], but none of them, including Object[] are real classes, they're
    > >>synthesized by the JVM.

    > >
    > >
    > > That's not right. An array of primitives can't be derived from Object[].

    >
    > Ah, you're right of course, I forgot about those.
    >
    > So there's not even a common superclass for all array types (except for Object).


    Yep, Object is the only common superclass for array types. That's why System.arraycopy()
    takes its array arguments as Object ;^)

    --
    Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
    ==============================================================
    * The Ultimate DBMS is here!
    * FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
    Lee Fesperman, Oct 29, 2004
    #15
  16. VisionSet coughed up:
    > "Michael Borgwardt" <> wrote in message
    > news:...
    >
    >>> I suppose I meant it doesn't have its *OWN* public API.

    >>
    >> Actually, it has. Unlike Object instances, arrays have the public
    >> length field and a public clone() method. See Java Language
    >> Specification section 10.7

    >
    > I wonder why they didn't make it a length() method - would feel more
    > intuitive especially since you stumble across this very early in
    > learning the language.
    >
    > How could they have included the Array type in the javadocs?



    It's funny how an innocuous post like yours get come under such fire! LOL.
    Welcome to Anal Quote International. I'm an offender too. In fact, I liked
    it so much I bought the company.

    I've always suspected that .length is a primitive and not an object is
    because of a few things:

    1. The array length is not something run-time
    computable. The length is indeed immutable.

    2. Efficiency (speed).

    3. There is no need for it to be synchronized, as
    a single primitive read is defined as atomic.

    I've always wondered why there wasn't a specific class called Array that
    /meant/ the array (forget he existing Array and Arrays classes). It would
    allow us to use instanceof, instead of calling a dedicated method to check
    an object for "arrayness". Furthermore, it might allow for some interesting
    subclasses.


    --
    Unix users who vehemently argue that the "ln" command has its arguments
    reversed do not understand much about the design of the utilities. "ln
    arg1 arg2" sets the arguments in the same order as "mv arg1 arg2".
    Existing file argument to non-existing argument. And in fact, mv
    itself is implemented as a link followed by an unlink.
    Thomas G. Marshall, Apr 30, 2005
    #16
  17. Thomas G. Marshall coughed up:
    > VisionSet coughed up:
    >> "Michael Borgwardt" <> wrote in message
    >> news:...
    >>
    >>>> I suppose I meant it doesn't have its *OWN* public API.
    >>>
    >>> Actually, it has. Unlike Object instances, arrays have the public
    >>> length field and a public clone() method. See Java Language
    >>> Specification section 10.7

    >>
    >> I wonder why they didn't make it a length() method - would feel more
    >> intuitive especially since you stumble across this very early in
    >> learning the language.
    >>
    >> How could they have included the Array type in the javadocs?

    >
    >
    > It's funny how an innocuous post like yours get come under such fire!
    > LOL. Welcome to Anal Quote International. I'm an offender too. In
    > fact, I liked it so much I bought the company.
    >
    > I've always suspected that .length is a primitive and not an object



    {SCREEEEEEEECHING brake, then collision, then rolling hub cap sounds all go
    here}

    I obviously meant "and not a method".



    > is because of a few things:
    >
    > 1. The array length is not something run-time
    > computable. The length is indeed immutable.
    >
    > 2. Efficiency (speed).
    >
    > 3. There is no need for it to be synchronized, as
    > a single primitive read is defined as atomic.
    >
    > I've always wondered why there wasn't a specific class called Array
    > that /meant/ the array (forget he existing Array and Arrays classes).
    > It would allow us to use instanceof, instead of calling a dedicated
    > method to check an object for "arrayness". Furthermore, it might
    > allow for some interesting subclasses.




    --
    Unix users who vehemently argue that the "ln" command has its arguments
    reversed do not understand much about the design of the utilities. "ln
    arg1 arg2" sets the arguments in the same order as "mv arg1 arg2".
    Existing file argument to non-existing argument. And in fact, mv
    itself is implemented as a link followed by an unlink.
    Thomas G. Marshall, Apr 30, 2005
    #17
  18. Oscar kind coughed up:
    > Niels Dybdahl <-graphics.com> wrote:
    >>
    >> You can do something similar, if you use Vectors instead of arrays.

    >
    > Or better yet, use a List (and a class that implements this
    > interface). Since the new Collections framework came out, I consider
    > it a good idea not to use a Vector, unless:
    > - I know the subtle differences between a Vector and a List
    > - A List doesn't suffice, and I need the specifics of a Vector



    There is only one good reason to use Vector.

    The vector methods are synchronized. Now you can get a synchronized
    arraylist at runtime, but you cannot get one at compile time for extension
    into a subclass. For reasons that no one can adequately explain, the
    classes responsible for such things are imbedded within Collections.java
    with /package scope/.

    If you /were/ to extend ArrayList, you would have to re-implement every
    single method just to put the synchronized keyword in:

    public synchronized void <method name>(<arg list>)
    {
    super.<method name>(<arg list>);
    }

    The <> are of course not generics, but just a syntax for the example...





    --
    Unix users who vehemently argue that the "ln" command has its arguments
    reversed do not understand much about the design of the utilities. "ln
    arg1 arg2" sets the arguments in the same order as "mv arg1 arg2".
    Existing file argument to non-existing argument. And in fact, mv
    itself is implemented as a link followed by an unlink.
    Thomas G. Marshall, Apr 30, 2005
    #18
  19. Thomas G. Marshall wrote:
    >
    > There is only one good reason to use Vector.
    >
    > The vector methods are synchronized.


    The second reason is 1.1 compatibility.

    --
    Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
    ==============================================================
    * The Ultimate DBMS is here!
    * FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
    Lee Fesperman, Apr 30, 2005
    #19
  20. Thomas Weidenfeller, May 2, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    614
    John Machin
    Jul 13, 2005
  2. HYRY
    Replies:
    10
    Views:
    599
    Bruno Desthuilliers
    Sep 26, 2007
  3. Victor Bazarov

    Re: Append one enum to another?

    Victor Bazarov, May 8, 2009, in forum: C++
    Replies:
    7
    Views:
    512
    Pascal J. Bourguignon
    May 15, 2009
  4. Ishmael

    Append one file to another?

    Ishmael, Jun 23, 2009, in forum: C Programming
    Replies:
    5
    Views:
    1,718
    user923005
    Jun 23, 2009
  5. Robert Cohen
    Replies:
    3
    Views:
    269
    Andrew Durstewitz
    Jul 15, 2003
Loading...

Share This Page