茫茫網海中的冷日 - 對這文章發表回應
茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1675742 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
PIMG_00306.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2019/1/26 4:40:37
Java String array out of existing String array

I have a Java String array:
public static final String[] FIELDS_NAMES = { "Albert", "Berta", "Carl" };
public static final String[] FIELDS_NUMBERS = { "123", "456", "789" };

and would like to create a third constant out of the one I already have. Currently I do it by repeating everything:
public static final String[] FIELDS_ALL = {
    "Albert", "Berta", "Carl", "123", "456", "789"
};

But what I really want is this:
public static final String[] FIELDS_ALL = {FIELDS_NAMES, FIELDS_NUMBERS};

Any idea how to do that in Java? Obviously I do not want to run any loops to shuffle things around...

--------------------------------------------------------------------------------

One way to concatenate string arrays:
String[] FIELDS_ALL = ArrayUtils.addAll(FIELDS_NAMES, FIELDS_NAMES);

EDIT Note: as @Malachi has mentioned, This uses Apache Commons Lang Library
EDIT Without using any external libs:

you can use this generic method to do so:
String[] join(String[]... arrays)
{
  // calculate size of target array
  int size = 0;
  for (String[] array : arrays)
  {
    size += array.length;
  }

  // create list of appropriate size
  java.util.List list = new java.util.ArrayList(size);

  // add arrays
  for (String[] array : arrays)
  {
    list.addAll(java.util.Arrays.asList(array));
  }

  // create and return final array
 return list.toArray(new String[size]);
}


--------------------------------------------------------------------------------

Although using Apache Commons Lang as suggested by other answer is the easiest, there are some way using Java built-in syntax which kind of does the work, without creating separate utility method.
public static final String[] FIELDS_ALL =
    new ArrayList<String>() {{
            addAll(Arrays.asList(FIELDS_NAMES));
            addAll(Arrays.asList(FIELDS_NUMBERS));
        }}.toArray(new String[0]);

(In case the syntax looks strange to other people: I am creating a anonymous class, which is a child class of ArrayList, and making use of initializer to call addAll to populate the content of the ArrayList. )

--------------------------------------------------------------------------------

Use system.arraycopy:
String[] concatArray(String[] A, String[] B) {
   int aLen = A.length;
   int bLen = B.length;
   int cLen = aLen+bLen;
   String[] C = new T[cLen];
   System.arraycopy(A, 0, C, 0, aLen);
   System.arraycopy(B, 0, C, aLen, bLen);
   return C;
}


--------------------------------------------------------------------------------

Can Use:
String[] FIELDS_NAMES = { "Albert", "Berta", "Carl" };
        String[] FIELDS_NUMBERS = { "123", "456", "789" };

        String[] FIELDS_ALL = new String[FIELDS_NAMES.length + FIELDS_NUMBERS.length] ;
        System.arraycopy(FIELDS_NAMES, 0, FIELDS_ALL, 0, FIELDS_NAMES.length);
        System.arraycopy(FIELDS_NUMBERS, 0, FIELDS_ALL, FIELDS_NAMES.length, FIELDS_NUMBERS.length);
        for (int j = 0; j < FIELDS_ALL.length; j++) {
            System.out.println(FIELDS_ALL[j]);
        }



原文出處:Java String array out of existing String array - Stack Overflow


冷日補充:

冷日用的方法就是『system.arraycopy』!

簡單好用又不需要額外的 Lib!

內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|