集册 Java实例教程 将元素添加到数组末尾

将元素添加到数组末尾

欢马劈雪     最近更新时间:2020-01-02 10:19:05

536
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
将元素添加到数组末尾
/* from 时 代 J a v a - nowjava.com*/


//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)));

    }


    public static String[] addElementToEndOfArray(String[] attributes,

            String element) {

        String[] attributesPlus = new String[attributes.length + 1];

        System.arraycopy(attributes, 0, attributesPlus, 0,/*from N o  w  J a v a . c o m - 时  代  Java*/

                attributes.length);

        attributesPlus[attributes.length] = element;

        attributes = attributesPlus;

        return attributes;

    }


    public static Object[] addElementToEndOfArray(Object[] attributes,

            Object element) {

        Object[] attributesPlus = new 
展开阅读全文