add Element To End Of Array - Java Collection Framework

Java examples for Collection Framework:Array Element

Description

add Element To End Of Array

Demo Code


//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String[] attributes = new String[] { "1", "abc", "level", null,
                "book2s.com", "asdf 123" };
        String element = "book2s.com";
        System.out.println(java.util.Arrays
                .toString(addElementToEndOfArray(attributes, element)));
    }// w  ww . j a v  a  2 s .  c o m

    public static String[] addElementToEndOfArray(String[] attributes,
            String element) {
        String[] attributesPlus = new String[attributes.length + 1];
        System.arraycopy(attributes, 0, attributesPlus, 0,
                attributes.length);
        attributesPlus[attributes.length] = element;
        attributes = attributesPlus;
        return attributes;
    }

    public static Object[] addElementToEndOfArray(Object[] attributes,
            Object element) {
        Object[] attributesPlus = new Object[attributes.length + 1];
        System.arraycopy(attributes, 0, attributesPlus, 0,
                attributes.length);
        attributesPlus[attributes.length] = element;
        attributes = attributesPlus;
        return attributes;
    }
}

Related Tutorials