Forum Stats

  • 3,814,524 Users
  • 2,258,880 Discussions
  • 7,892,765 Comments

Discussions

Array Copy for Any Objects

807606
807606 Member Posts: 49,741 Green Ribbon
edited Mar 3, 2007 10:38AM in Java Programming
Hi all,

I wrote a function which is used to concat two Arrays. HOwever, I need to specifiy the data type in the function, it's not OO and absolutely not dynamic, how can I improve it ?
    public static Object concatDualArray(Object[] A, Object[] B) {
        StandingData[] C= new StandingData[A.length+B.length];
        System.arraycopy(A, 0, C, 0, A.length);
        System.arraycopy(B, 0, C, A.length, B.length);
      
        return C;
     } 
I summon that function from my core function, which is
        StandingData[] trancheStatusListData = (StandingData[])
                TrancheUtility.concatDualArray((Object[])trancheApprovedStatusListData, (Object[])trancheApprovalStatusListData);
Is there any reflection method can let my function looks more OO ?

Thanks for helping me

Transistor

Comments

  • JosAH
    JosAH Member Posts: 7,698
    Personally I wouldn't go for the array approach in the first place, because
    their type can not be parameterized; e.g. the following is simply not
    possible, no matter how cute it looks:
    public static<T> T[] concatDualArray(T[] A, T[] B) {
       T[] C= new T[A.length+B.length];
       System.arraycopy(A, 0, C, 0, A.length);
       System.arraycopy(B, 0, C, A.length, B.length);
          
       return C;
    } 
    Arrays are very non-OO; why not stick to a List<T> instead?

    kind regards,

    Jos
This discussion has been closed.