Java Array Append addToArray(String[] items, String str)

Here you can find the source of addToArray(String[] items, String str)

Description

Adds a string to a string array.

License

Open Source License

Parameter

Parameter Description
items The array to append data in
str The string to append

Return

The modified array

Declaration

public static String[] addToArray(String[] items, String str) 

Method Source Code


//package com.java2s;
/*//from  w  ww  . j  a va  2  s. c om
 * Copyright 2007-2012 The Europeana Foundation
 *
 *  Licenced under the EUPL, Version 1.1 (the "Licence") and subsequent versions as approved
 *  by the European Commission;
 *  You may not use this work except in compliance with the Licence.
 * 
 *  You may obtain a copy of the Licence at:
 *  http://joinup.ec.europa.eu/software/page/eupl
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under
 *  the Licence is distributed on an "AS IS" basis, without warranties or conditions of
 *  any kind, either express or implied.
 *  See the Licence for the specific language governing permissions and limitations under
 *  the Licence.
 */

import java.util.ArrayList;
import java.util.Arrays;

import java.util.List;

public class Main {
    public static final String[] EMPTY_ARRAY = new String[] {};

    /**
     * Adds a string to a string array. If the array is null it creates it
     * 
     * @param items
     *            The array to append data in
     * @param str
     *            The string to append
     * @return The modified array
     */
    public static String[] addToArray(String[] items, String str) {
        List<String> itemList;
        if (items == null) {
            itemList = new ArrayList<String>();
        } else {
            itemList = new ArrayList<String>(Arrays.asList(items));
        }
        if (str != null) {
            itemList.add(str);
        }
        return itemList.toArray(new String[itemList.size()]);
    }

    /**
     * Convert a list to array
     * 
     * @param list
     * @return
     */
    public static String[] toArray(List<String> list) {
        if (list != null && list.size() > 0) {
            return list.toArray(new String[list.size()]);
        }
        return EMPTY_ARRAY;
    }

    public static String[] toArray(String... items) {
        return items;
    }
}

Related

  1. addToArray(final int[] array, int index, final String csvString, final String delim)
  2. addToArray(int[] a, int value)
  3. appendArray(final byte[] buffer1, final byte[] buffer2)
  4. appendArray(int[][] array, int[] staple)
  5. appendArray(Object[] arr, Object obj)
  6. appendArray(Object[] array1, Object[] array2)