数组指定位置插入数据--JAVA

 《漫画算法--小灰的算法之旅》

每天学习一点数据结构,既是小灰的算法之旅,也是本人的算法之旅。

当前代码实现的是数组的指定位置插入数据的代码。

package com.java.array;

/**
 * 向数组插入数据的思想
 * 1、需要判断插入位置的下标是否是数组的正常范围之内
 * 2、如果数组实际元素达到数组容量的上限,则对数组进行扩容
 * 3、从右向左循环,将元素逐个向右移动,腾出的位置插入新的元素
 *
 * 当前程序:不允许随意插入任意位置的数据,不然报错。
 */
public class ArrayInsert {
    private int[] arr;
    private int size;

    public ArrayInsert(int capacity) {
        this.arr = new int[capacity];
        this.size = 0;
    }

    public static void main(String[] args) throws Exception {
        ArrayInsert aInsert = new ArrayInsert(4);
        aInsert.insert(0, 3);
        aInsert.insert(1, 9);
        aInsert.insert(2, 5);
        aInsert.insert(3, 7);
        aInsert.insert(2, 6);
        aInsert.insert(1, 8);
        aInsert.outArr();
    }

    /**
     * @param index   插入的下标
     * @param element 插入的元素
     * @throws Exception
     */
    private void insert(int index, int element) throws Exception {
        if (index < 0 || index > size) {
            throw new Exception("Out of Index");
        }
        if (size >= arr.length) {
            resize();
        }
        for (int i = size - 1; i >= index; i--) {
            arr[i + 1] = arr[i];
        }
        arr[index] = element;
        size++;
    }

    public void resize() {
        int[] newArr = new int[arr.length * 2];
        System.arraycopy(arr, 0, newArr, 0, arr.length);
        arr = newArr;
    }

    private void outArr() {
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}

System.arraycopy()源码:

 * @param      src      the source array.
 * @param      srcPos   starting position in the source array.
 * @param      dest     the destination array.
 * @param      destPos  starting position in the destination data.
 * @param      length   the number of array elements to be copied.
 * @exception  IndexOutOfBoundsException  if copying would cause
 *               access of data outside array bounds.
 * @exception  ArrayStoreException  if an element in the <code>src</code>
 *               array could not be stored into the <code>dest</code> array
 *               because of a type mismatch.
 * @exception  NullPointerException if either <code>src</code> or
 *               <code>dest</code> is <code>null</code>.
 */
public static native void arraycopy(Object src,  int  srcPos,Object dest, int destPos,int length);

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 中,字符串数组是一种固定长度的数据结构,无法像 ArrayList 一样直接插入数据。但可以通过以下两种方式实现类似的效果: 1. 使用 System.arraycopy 方法 通过 System.arraycopy 方法可以将原数组中的数据复制到一个新的数组中,并在指定位置插入新的数据。示例如下: ``` String[] array = {"A", "B", "C", "D", "E"}; String[] newArray = new String[array.length + 1]; int insertIndex = 2; String insertValue = "X"; System.arraycopy(array, 0, newArray, 0, insertIndex); newArray[insertIndex] = insertValue; System.arraycopy(array, insertIndex, newArray, insertIndex + 1, array.length - insertIndex); ``` 这里定义了原数组 array,新数组 newArray 和要插入数据 insertValue,以及插入位置 insertIndex。首先使用 System.arraycopy 方法将原数组中 0 到 insertIndex 的数据复制到新数组中,然后在 insertIndex 处插入数据,最后将原数组中 insertIndex 之后的数据复制到新数组中 insertIndex + 1 的位置之后。 2. 使用 ArrayList 转换 可以将字符串数组转换为 ArrayList,使用 ArrayList 的 add 方法插入数据,最后再将 ArrayList 转换回字符串数组。示例如下: ``` String[] array = {"A", "B", "C", "D", "E"}; List<String> list = new ArrayList<>(Arrays.asList(array)); int insertIndex = 2; String insertValue = "X"; list.add(insertIndex, insertValue); String[] newArray = list.toArray(new String[0]); ``` 这里先将字符串数组 array 转换为 ArrayList,然后使用 add 方法在指定位置插入数据,最后再将 ArrayList 转换回字符串数组 newArray。 注意:以上两种方法均会创建新的数组或集合对象,而不是在原数组中直接插入数据

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值