• 技术文章 >java >java数组

    java数组插入元素的三种方法

    小妮浅浅小妮浅浅2021-03-22 10:21:59原创16363

    本教程操作环境:windows7系统、java10版,DELL G3电脑。

    1、使用 insertElement () 方法向数组插入元素

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    import java.util.Arrays;

    public class Test{

          

          

          

        public static void main(String args[]) throws Exception {

          

          

          

            int array[] = {

          

          

          2, 5, -2, 6, -3, 8, 0, -7, -9, 4 };

            Arrays.sort(array);

            int index = Arrays.binarySearch(array, 1);

            System.out.println("元素 1 所在位置(负数为不存在):"

                    + index);

            int newIndex = -index - 1;

            array = insertElement(array, 1, newIndex);

            System.out.println("添加元素1后:"+Arrays.toString(array));

        }

      

        private static int[] insertElement(int original[],

                                           int element, int index) {

          

          

          

            int length = original.length;

            int destination[] = new int[length + 1];

            System.arraycopy(original, 0, destination, 0, index);

            destination[index] = element;

            System.arraycopy(original, index, destination, index

                    + 1, length - index);

            return destination;

        }

    }

    /* 输出结果:

    元素 1 所在位置(负数为不存在):-6

    添加元素1:[-9, -7, -3, -2, 0, 1, 2, 4, 5, 6, 8]

     */

    2、把数组转化为集合,向集合中添加元素,再将集合转化为数组

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    import java.util.*;

    public class Test{

          

          

          

        public static void main(String[] args) {

          

          

          

            String[] arr = {

          

          

         "ID", "姓名"};

            // 将数组转化为集合 1

            List<String> list1 = Arrays.asList(arr);

            List<String> list2 = new ArrayList<>();

            // 定义集合 2 、并向其中添加元素: 性别

            list2.add("性别");

            List<String> List = new ArrayList<String>();

            // 定义新集合、将集合1、2中的元素添加到新集合

            List.addAll(list1);

            List.addAll(list2);

            // 将新集合转化回新数组

            String[] newArr = List.toArray(new String[List.size()]);

            System.out.println(Arrays.toString(newArr));

        }

    }

    /* 输出结果: [ID, 姓名, 性别]  */

    3、创建一个新数组,新数组的大小为旧数组大小+1,把旧数组里的元素copy一份进新数组,并把要添加的元素添加进新数组即可。

    以上就是java数组插入元素的三种方法,大家可以用手上的数组进行试验,在三种方法中选择最适合自己使用的,并加强记忆。更多Java学习指路:java数组

    专题推荐:java数组
    上一篇:java数组如何插入元素并快捷排序? 下一篇:java可变参数列表如何填充数组?

    相关文章推荐

    • java数组的概念理解• java数组初始化方式• java数组内存的探究• java数组进行翻转的方法有哪些• java数组中有哪些面试题• java数组中如何查找元素的位置?• java数组如何插入元素并快捷排序?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网