📌  相关文章
📜  从Java中的数组中删除特定索引处的元素

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

从Java中的数组中删除特定索引处的元素

给定一个固定长度的数组。任务是从数组中删除特定索引处的元素。

例子:

Input: arr[] = { 1, 2, 3, 4, 5 }, index = 2
Output: arr[] = { 1, 2, 4, 5 }

Input: arr[] = { 4, 5, 9, 8, 1 }, index = 3
Output: arr[] = { 4, 5, 9, 1 }

数组是包含一组元素的数据结构。通常,这些元素都是相同的数据类型,例如整数或字符串。数组通常在计算机程序中用于组织数据,以便可以快速排序或搜索相关的一组值。数组的所有项目都存储在连续的内存位置。

方法

有许多方法可以检查此 Array 中是否存在特定Java。这些都是 -

  • 使用另一个数组
  • 使用Java 8 流
  • 使用数组列表
  • 使用 System.arraycopy() 方法

1. 使用另一个数组(朴素或基本方法)

基本方法包括在指定索引处查找元素,然后删除该元素。其余元素被复制到一个新数组中。这将导致数组的大小比原始数组小一。下面是上述方法的实现:

Java
// Java program to remove an element
// from a specific index from an array
 
import java.util.Arrays;
 
class GFG {
 
    // Function to remove the element
    public static int[] removeTheElement(int[] arr, int index)
    {
 
        // If the array is empty
        // or the index is not in array range
        // return the original array
        if (arr == null || index < 0
            || index >= arr.length) {
 
            return arr;
        }
 
        // Create another array of size one less
        int[] anotherArray = new int[arr.length - 1];
 
        // Copy the elements except the index
        // from original array to the other array
        for (int i = 0, k = 0; i < arr.length; i++) {
 
            // if the index is
            // the removal element index
            if (i == index) {
                continue;
            }
 
            // if the index is not
            // the removal element index
            anotherArray[k++] = arr[i];
        }
 
        // return the resultant array
        return anotherArray;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Get the array
        int[] arr = { 1, 2, 3, 4, 5 };
 
        // Print the resultant array
        System.out.println("Original Array: "
                           + Arrays.toString(arr));
 
        // Get the specific index
        int index = 2;
 
        // Print the index
        System.out.println("Index to be removed: " + index);
 
        // Remove the element
        arr = removeTheElement(arr, index);
 
        // Print the resultant array
        System.out.println("Resultant Array: "
                           + Arrays.toString(arr));
    }
}


Java
// Java program to remove an element
// from a specific index from an array
 
import java.util.Arrays;
import java.util.stream.IntStream;
 
class GFG {
 
    // Function to remove the element
    public static int[] removeTheElement(int[] arr, int index)
    {
 
        // If the array is empty
        // or the index is not in array range
        // return the original array
        if (arr == null
            || index < 0
            || index >= arr.length) {
 
            return arr;
        }
 
        // return the resultant array
        return IntStream.range(0, arr.length)
            .filter(i -> i != index)
            .map(i -> arr[i])
            .toArray();
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Get the array
        int[] arr = { 1, 2, 3, 4, 5 };
 
        // Print the resultant array
        System.out.println("Original Array: "
                        + Arrays.toString(arr));
 
        // Get the specific index
        int index = 2;
 
        // Print the index
        System.out.println("Index to be removed: "
                        + index);
 
        // Remove the element
        arr = removeTheElement(arr, index);
 
        // Print the resultant array
        System.out.println("Resultant Array: "
                        + Arrays.toString(arr));
    }
}


Java
// Java program to remove an element
// from a specific index from an array
 
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
 
class GFG {
 
    // Function to remove the element
    public static int[] removeTheElement(int[] arr, int index)
    {
 
        // If the array is empty
        // or the index is not in array range
        // return the original array
        if (arr == null
            || index < 0
            || index >= arr.length) {
 
            return arr;
        }
 
        // Create ArrayList from the array
        List arrayList = IntStream.of(arr)
                                    .boxed()
                                    .collect(Collectors.toList());
 
        // Remove the specified element
        arrayList.remove(index);
 
        // return the resultant array
        return arrayList.stream()
            .mapToInt(Integer::intValue)
            .toArray();
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Get the array
        int[] arr = { 1, 2, 3, 4, 5 };
 
        // Print the resultant array
        System.out.println("Original Array: "
                        + Arrays.toString(arr));
 
        // Get the specific index
        int index = 2;
 
        // Print the index
        System.out.println("Index to be removed: "
                        + index);
 
        // Remove the element
        arr = removeTheElement(arr, index);
 
        // Print the resultant array
        System.out.println("Resultant Array: "
                        + Arrays.toString(arr));
    }
}


Java
// Java program to remove an element
// from a specific index from an array
 
import java.util.Arrays;
 
class GFG {
 
    // Function to remove the element
    public static int[] removeTheElement(int[] arr, int index)
    {
 
        // If the array is empty
        // or the index is not in array range
        // return the original array
        if (arr == null
            || index < 0
            || index >= arr.length) {
 
            return arr;
        }
 
        // Create another array of size one less
        int[] anotherArray = new int[arr.length - 1];
 
        // Copy the elements from starting till index
        // from original array to the other array
        System.arraycopy(arr, 0, anotherArray, 0, index);
 
        // Copy the elements from index + 1 till end
        // from original array to the other array
        System.arraycopy(arr, index + 1,
                        anotherArray, index,
                        arr.length - index - 1);
 
        // return the resultant array
        return anotherArray;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Get the array
        int[] arr = { 1, 2, 3, 4, 5 };
 
        // Print the resultant array
        System.out.println("Original Array: "
                        + Arrays.toString(arr));
 
        // Get the specific index
        int index = 2;
 
        // Print the index
        System.out.println("Index to be removed: "
                        + index);
 
        // Remove the element
        arr = removeTheElement(arr, index);
 
        // Print the resultant array
        System.out.println("Resultant Array: "
                        + Arrays.toString(arr));
    }
}



输出
Original Array: [1, 2, 3, 4, 5]
Index to be removed: 2
Resultant Array: [1, 2, 4, 5]

2. 使用Java 8 流

方法:

  • 获取数组和索引。
  • 使用 IntStream.range() 方法将数组转换为 IntStream。
  • 使用 filter() 方法删除指定的索引元素。
  • 使用 map() 和 toArray() 方法映射并形成一个新的过滤元素数组。
  • 返回形成的数组。

下面是上述方法的实现。

Java

// Java program to remove an element
// from a specific index from an array
 
import java.util.Arrays;
import java.util.stream.IntStream;
 
class GFG {
 
    // Function to remove the element
    public static int[] removeTheElement(int[] arr, int index)
    {
 
        // If the array is empty
        // or the index is not in array range
        // return the original array
        if (arr == null
            || index < 0
            || index >= arr.length) {
 
            return arr;
        }
 
        // return the resultant array
        return IntStream.range(0, arr.length)
            .filter(i -> i != index)
            .map(i -> arr[i])
            .toArray();
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Get the array
        int[] arr = { 1, 2, 3, 4, 5 };
 
        // Print the resultant array
        System.out.println("Original Array: "
                        + Arrays.toString(arr));
 
        // Get the specific index
        int index = 2;
 
        // Print the index
        System.out.println("Index to be removed: "
                        + index);
 
        // Remove the element
        arr = removeTheElement(arr, index);
 
        // Print the resultant array
        System.out.println("Resultant Array: "
                        + Arrays.toString(arr));
    }
}
输出
Original Array: [1, 2, 3, 4, 5]
Index to be removed: 2
Resultant Array: [1, 2, 4, 5]

3. 使用数组列表

方法:

  • 获取数组和索引。
  • 用数组元素组成一个 ArrayList。
  • 使用 remove() 方法删除指定的索引元素。
  • 使用 mapToInt() 和 toArray() 方法形成 ArrayList 的新数组。
  • 返回形成的数组。

下面是上述方法的实现。

Java

// Java program to remove an element
// from a specific index from an array
 
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
 
class GFG {
 
    // Function to remove the element
    public static int[] removeTheElement(int[] arr, int index)
    {
 
        // If the array is empty
        // or the index is not in array range
        // return the original array
        if (arr == null
            || index < 0
            || index >= arr.length) {
 
            return arr;
        }
 
        // Create ArrayList from the array
        List arrayList = IntStream.of(arr)
                                    .boxed()
                                    .collect(Collectors.toList());
 
        // Remove the specified element
        arrayList.remove(index);
 
        // return the resultant array
        return arrayList.stream()
            .mapToInt(Integer::intValue)
            .toArray();
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Get the array
        int[] arr = { 1, 2, 3, 4, 5 };
 
        // Print the resultant array
        System.out.println("Original Array: "
                        + Arrays.toString(arr));
 
        // Get the specific index
        int index = 2;
 
        // Print the index
        System.out.println("Index to be removed: "
                        + index);
 
        // Remove the element
        arr = removeTheElement(arr, index);
 
        // Print the resultant array
        System.out.println("Resultant Array: "
                        + Arrays.toString(arr));
    }
}
输出
Original Array: [1, 2, 3, 4, 5]
Index to be removed: 2
Resultant Array: [1, 2, 4, 5]

4. 使用 System.arraycopy() 方法

方法:

  • 获取数组和索引。
  • 创建一个大小比原始数组小一的新数组。
  • 使用 System.arraycopy() 将元素从原始数组从开始到索引复制到另一个数组。
  • 使用 System.arraycopy() 将元素从 index + 1 复制到原始数组的末尾到另一个数组。
  • 返回形成的数组。

下面是上述方法的实现。

Java

// Java program to remove an element
// from a specific index from an array
 
import java.util.Arrays;
 
class GFG {
 
    // Function to remove the element
    public static int[] removeTheElement(int[] arr, int index)
    {
 
        // If the array is empty
        // or the index is not in array range
        // return the original array
        if (arr == null
            || index < 0
            || index >= arr.length) {
 
            return arr;
        }
 
        // Create another array of size one less
        int[] anotherArray = new int[arr.length - 1];
 
        // Copy the elements from starting till index
        // from original array to the other array
        System.arraycopy(arr, 0, anotherArray, 0, index);
 
        // Copy the elements from index + 1 till end
        // from original array to the other array
        System.arraycopy(arr, index + 1,
                        anotherArray, index,
                        arr.length - index - 1);
 
        // return the resultant array
        return anotherArray;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Get the array
        int[] arr = { 1, 2, 3, 4, 5 };
 
        // Print the resultant array
        System.out.println("Original Array: "
                        + Arrays.toString(arr));
 
        // Get the specific index
        int index = 2;
 
        // Print the index
        System.out.println("Index to be removed: "
                        + index);
 
        // Remove the element
        arr = removeTheElement(arr, index);
 
        // Print the resultant array
        System.out.println("Resultant Array: "
                        + Arrays.toString(arr));
    }
}
输出
Original Array: [1, 2, 3, 4, 5]
Index to be removed: 2
Resultant Array: [1, 2, 4, 5]