Java 从数组中删除所有奇数元素

Java 从数组中删除所有奇数元素

数组是一组类似的数据类型(如整数、浮点数或字符),它们在连续的内存位置中存储在一起,并可以使用索引或下标访问。

数组通常用于在程序中存储和操作大量的数据。它们提供了一种便捷的方式将相关的数据分组在一起,并允许高效地访问数组的各个元素。

在这里,我们给出了一个包含偶数和奇数元素的数组,根据问题的陈述,我们需要从该整数数组中删除所有奇数元素。

让我们开始!

展示一些示例

示例-1

Original array: [23, 12, 45, 65, 34, 23, 54, 76, 20]
Array after deleting odd elements: [12, 34, 54, 76, 20]

实例-2

Original array: [-22, 45, 23, 65, -9, 26]
Array after deleting odd elements: [-22, 26]

实例-3

Original array: [123, 345, 657, 342, 344]
Array after deleting odd elements: [342, 344]

步骤

步骤-1

  • 步骤-1 - 创建一个新数组。

  • 步骤-2 - 遍历输入数组,并将偶数元素添加到新数组中。

  • 步骤-3 - 创建一个只包含偶数元素的新数组并返回。

  • 步骤-4 - 该程序打印出删除奇数元素后的原始数组和结果数组。

  • 步骤-5 - 它使用Java中的一个方法来删除整数数组中的奇数元素。

步骤-2

  • 步骤-1 - 创建一个包含元素的ArrayList。

  • 步骤-2 - 遍历输入数组,并将偶数元素添加到ArrayList中。

  • 步骤-3 - 创建一个只包含偶数元素的新ArrayList并返回。

  • 步骤-4 - 该程序打印出删除奇数元素后的原始数组和结果ArrayList。

  • 步骤-5 - 它使用ArrayList来删除整数数组中的奇数元素。

语法

1. Java中的 length 方法返回给定数组的长度。

以下是其语法:

inputArray.lenght

其中,’inputArray’代表给定的数组。

2. size() 方法返回集合中的元素数量,例如 ArrayList 或 LinkedList。它是Java中的内置方法,可以用于在任何给定时间确定集合的大小。

以下是它的语法 –

evenList.size()

其中,’evenList’指的是ArrayList,用于存储输入数组中的偶数元素。

3. get() 方法返回集合中指定索引处的元素,例如ArrayList或LinkedList。这是Java中的一个内置方法,可用于通过索引访问集合中的特定元素。

下面是其语法 –

evenList.size()

其中’evenList’是ArrayList,用来存储输入数组中的偶数元素。

多种方法

我们提供了不同的解决方案。

  • 使用新数组

  • 使用ArrayList

让我们逐一查看程序及其输出。

方法1:使用新数组

在这种方法中,程序将初始化数组元素。然后通过将数组作为参数传递给用户定义的方法,并在该方法内部根据算法-1使用新数组删除所有奇数元素。

示例

import java.util.Arrays;
public class Main {
   public static void main(String[] args) {

      // Initialize the array with some values
      int[] inputArray = {1, 2, 3, 4, 5, 6, 7, 8, 9};

      // Call the delOddElmts method to remove the odd elements from the array
      int[] resultArray = delOddElmts(inputArray);

      // Print the resulting array
      System.out.println("Original array: " + Arrays.toString(inputArray));
      System.out.println("Array after deleting odd elements: " + Arrays.toString(resultArray));
   }

   //User Defined Method that deletes all odd elements from an array of integers
   public static int[] delOddElmts(int[] arr) {

      // Create a new array to store the even elements from the input array
      int[] evenArr = new int[arr.length];
      int j = 0;

      // Iterate through the input array and add the even elements to the new array
      for (int i = 0; i < arr.length; i++) {
         if (arr[i] % 2 == 0) {
            evenArr[j] = arr[i];
            j++;
         }
      }

      // Create a new array with the exact size needed to store the even elements
      int[] result = new int[j];

      // Copy the even elements from the new array to the result array
      for (int i = 0; i < j; i++) {
         result[i] = evenArr[i];
      }

      // Return the result array
      return result;
   }
}

输出

Original array: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Array after deleting odd elements: [2, 4, 6, 8]

方法2:使用ArrayList

在这种方法中,数组元素将在程序中初始化。然后通过将数组作为参数传递给一个用户定义的方法,并在方法内部按照算法2的步骤,使用ArrayList删除数组中的所有奇数元素。

示例

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
   public static void main(String[] args) {

      // Initialize the array with some values
      int[] inputArray = {23,12,45,65,34,23,54,76,20};

      // Call the delOddElmts method to remove the odd elements from the array
      int[] resultArray = delOddElmts(inputArray);

      // Print the resulting array
      System.out.println("Original array: " + Arrays.toString(inputArray));
      System.out.println("Array after deleting odd elements: " + Arrays.toString(resultArray));
   }

   //User Defined Method that deletes all odd elements from an array of integers
   public static int[] delOddElmts(int[] arr) {

      // Create an ArrayList to store the even elements from the input array
      List<Integer> evenList = new ArrayList<Integer>();

      // Iterate through the input array and add the even elements to the ArrayList
      for (int i = 0; i < arr.length; i++) {
         if (arr[i] % 2 == 0) {
            evenList.add(arr[i]);
         }
      }

      // Create a new array with the same size as the ArrayList
      int[] result = new int[evenList.size()];

      // Copy the elements from the ArrayList to the result array
      for (int i = 0; i < evenList.size(); i++) {
         result[i] = evenList.get(i);
      }

      // Return the result array
      return result;
   }
}

输出

Original array: [23, 12, 45, 65, 34, 23, 54, 76, 20]
Array after deleting odd elements: [12, 34, 54, 76, 20]

在本文中,我们通过使用Java编程语言,探讨了从数组中删除所有奇数元素的不同方法。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程