📌  相关文章
📜  Java如何在数组的特定位置插入元素

📅  最后修改于: 2022-05-13 01:58:09.262000             🧑  作者: Mango

Java如何在数组的特定位置插入元素

数组是存储在连续内存位置的项目的集合。在本文中,我们将了解如何在Java中向数组中插入元素
给定一个大小为n的数组arr ,本文介绍如何在此数组arr中的特定位置pos处插入元素x

方法一:
这是如何做到的。

  1. 首先获取要插入的元素,比如x
  2. 然后获取要插入此元素的位置,例如pos
  3. 创建一个大小比先前大小大一的新数组
  4. 将前一个数组中的所有元素复制到新数组中,直到位置 pos
  5. 在位置 pos 插入元素 x
  6. 在 pos 之后将前一个数组中的其余元素插入到新数组中

下面是上述方法的实现:

Java
// Java Program to Insert an element
// at a specific position in an Array
 
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
    // Function to insert x in arr at position pos
    public static int[] insertX(int n, int arr[],
                                int x, int pos)
    {
        int i;
 
        // create a new array of size n+1
        int newarr[] = new int[n + 1];
 
        // insert the elements from
        // the old array into the new array
        // insert all elements till pos
        // then insert x at pos
        // then insert rest of the elements
        for (i = 0; i < n + 1; i++) {
            if (i < pos - 1)
                newarr[i] = arr[i];
            else if (i == pos - 1)
                newarr[i] = x;
            else
                newarr[i] = arr[i - 1];
        }
        return newarr;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        int n = 10;
        int i;
 
        // initial array of size 10
        int arr[]
            = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 
        // print the original array
        System.out.println("Initial Array:\n"
                           + Arrays.toString(arr));
 
        // element to be inserted
        int x = 50;
 
        // position at which element
        // is to be inserted
        int pos = 5;
 
        // call the method to insert x
        // in arr at position pos
        arr = insertX(n, arr, x, pos);
 
        // print the updated array
        System.out.println("\nArray with " + x
                           + " inserted at position "
                           + pos + ":\n"
                           + Arrays.toString(arr));
    }
}


Java
// Java Program to Insert an element
// at a specific position in an Array
// using ArrayList
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class AddElementAtPositionInArray {
     
    // Method to add element at position
    private static void addElement(
        Integer[] arr, int element,
        int position)
    {
        // Converting array to ArrayList
        List list = new ArrayList<>(
            Arrays.asList(arr));
         
        // Adding the element at position
        list.add(position - 1, element);
         
        // Converting the list back to array
        arr = list.toArray(arr);
 
        // Printing the original array
        System.out.println("Initial Array:\n"
                        + Arrays.toString(arr));
 
        // Printing the updated array
        System.out.println("\nArray with " + element
                        + " inserted at position "
                        + position + ":\n"
                        + Arrays.toString(arr));
    }
     
    // Drivers Method
    public static void main(String[] args)
    {
        // Sample array
        Integer[] arr = { 1, 2, 3, 4, 5,
                        6, 7, 8, 9, 10 };
     
        // Element to be inserted
        int element = 50;
     
        // Position to insert
        int position = 5;
     
        // Calling the function to insert
        addElement(arr, element, position);
    }
}


输出:
Initial Array:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Array with 50 inserted at position 5:
[1, 2, 3, 4, 50, 5, 6, 7, 8, 9, 10]

方法二:
这是如何做到的。

  1. 首先获取要插入的元素,比如说元素
  2. 然后获取要插入此元素的位置,例如位置
  3. 将数组转换为ArrayList
  4. 使用list.add(position, element)位置添加元素
  5. ArrayList转换回数组并打印

下面是上述方法的实现:

Java

// Java Program to Insert an element
// at a specific position in an Array
// using ArrayList
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class AddElementAtPositionInArray {
     
    // Method to add element at position
    private static void addElement(
        Integer[] arr, int element,
        int position)
    {
        // Converting array to ArrayList
        List list = new ArrayList<>(
            Arrays.asList(arr));
         
        // Adding the element at position
        list.add(position - 1, element);
         
        // Converting the list back to array
        arr = list.toArray(arr);
 
        // Printing the original array
        System.out.println("Initial Array:\n"
                        + Arrays.toString(arr));
 
        // Printing the updated array
        System.out.println("\nArray with " + element
                        + " inserted at position "
                        + position + ":\n"
                        + Arrays.toString(arr));
    }
     
    // Drivers Method
    public static void main(String[] args)
    {
        // Sample array
        Integer[] arr = { 1, 2, 3, 4, 5,
                        6, 7, 8, 9, 10 };
     
        // Element to be inserted
        int element = 50;
     
        // Position to insert
        int position = 5;
     
        // Calling the function to insert
        addElement(arr, element, position);
    }
}
输出:
Initial Array:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Array with 50 inserted at position 5:
[1, 2, 3, 4, 50, 5, 6, 7, 8, 9, 10]