首页 > 编程知识 正文

数组删除元素的方法java,数组元素的删除java

时间:2023-05-06 09:54:33 阅读:179743 作者:1420

java数组删除数组元素

When we create an array in Java,wespecifyitsdatatypeandsize.thisisusedbyjvmtoallocatesthenecessarymemoryforarrayelements.therere

在Java中创建数组时,请指定数据类型和大小。 JVM使用它为数组元素分配所需的内存。 没有从数组中删除元素的特定方法。

1 .使用for循环从数组中删除元素(1. removinganelementfromarrayusingforloop ) )。 thismethodrequiresthecreationofanewarray.wecanuseforlooptopopulatethenewarraywithouttheelementwewanttoremove。

此方法需要创建新数组。 可以使用for循环填充新数组,而不删除要删除的元素。

package com.journaldev.java; import java.util.Arrays; public class main { publicstaticvoidmain (string [ ] args ) int [ ] arr=new int [ ] { 1,2,3,4,5 }; int [ ] arr _ new=new int [ arr.length-1 ]; int j=3; for(intI=0,k=0; iarr.length; I () if ) I!=j({ARR_new[k]=ARR[I]; k; } system.out.println (' before deletion : ' arrays.tostring ) ); system.out.println (after deletion : ) arrays.tostring (arr _ new ); } thecoderemovestheelementatindex3. thismethodsimplycopiesalltheelementsexceptheoneatindex3toa new array。

代码删除索引3处的元素。 此方法只将索引3处的元素以外的所有元素复制到新数组中。

2 .数组元素(2. deletinganarrayelementbyitsvalue )用该值删除Unlike the previous case,thiscodewilldeletetheementbasedonitsvalue .

与前者不同,代码根据其值删除元素。 无法应用于重复,因为您需要知道删除后阵列的大小。

package com.journaldev.java; import java.util.Arrays; public class main { publicstaticvoidmain (string [ ] args ) int [ ] arr=new int [ ] { 1,2,3,4,5 }; int [ ] arr _ new=new int [ arr.length-1 ]; int j=3; for(intI=0,k=0; iarr.length; I(if ) ARR[I]!=j({ARR_new[k]=ARR[I]; k; } system.out.println (' before deletion : ' arrays.tostring ) ); system.out.println (after deletion : ) arrays.tostring (arr _ new ); } theonlydifferencebetweenthisandthepreviouscaseisarr [ I ]!=j in the if condition in place of i!=j。

这种情况与前一种情况的唯一区别是在if条件下代替I!=j arr[i]!=j i!=j

3.当数组包含重复项时,通过其值删除元素 (3. Deleting element by its value when the array contains duplicates )

Performing deletion based on the value in case of duplicates requires using ArrayList. Since ArrayList doesn’t require the size to be now in advance, it allows us to expand dynamically.

在重复的情况下,根据值执行删除操作需要使用ArrayList。 由于ArrayList不需要现在预先指定大小,因此它允许我们动态扩展。

package com.journaldev.java;import java.util.ArrayList;import java.util.Arrays;public class Main { public static void main(String[] args) { int[] arr = new int[]{1,3,3,4,5}; ArrayList<Integer> arr_new = new ArrayList<>(); int j=3; for(int i=0;i<arr.length;i++){ if(arr[i]!=j){ arr_new.add(arr[i]); } } System.out.println("Before deletion :" + Arrays.toString(arr)); System.out.println("After deletion :" +arr_new); }} 4.移位相同数组中的元素 (4. Shifting elements in the same array)

This method involves shifting the elements in the same array. Shifting of elements replaces the element to be deleted by the element at the next index.

此方法涉及移动同一阵列中的元素。 元素的移动将替换下一个索引处要删除的元素。

package com.journaldev.java;import java.util.Arrays;public class Main { public static void main(String[] args) { int[] arr = new int[]{1,3,3,4,5}; int j=3; System.out.println("Before deletion :" + Arrays.toString(arr)); int count =0; for(int i = 0; i < arr.length; i++){ if(arr[i] == j){ count++; // shifting elements for(int k = i; k < arr.length - 1; k++){ arr[k] = arr[k+1]; } i--; // break; } } System.out.print("After Deletion :" ); for(int i = 0; i < arr.length-count; i++){ System.out.print(" " + arr[i]); } System.out.println(); System.out.println("Whole array :" + Arrays.toString(arr)); }}

Count variable indicates the number of elements deleted. This variable is essential to keep a track of index till which the array should be printed. This method takes care of duplicates as well.

Count变量指示删除的元素数。 该变量对于跟踪要打印数组的索引至关重要。 此方法也处理重复项。

5.从ArrayList中删除元素 (5. Deleting elements from ArrayList)

ArrayList is backed by arrays. The deletion of an element in the ArrayList is straight forward. It requires one simple call to an inbuilt function.

ArrayList由数组支持。 直接删除ArrayList中的元素。 它仅需简单调用一个内置函数。

package com.journaldev.java;import java.util.ArrayList;import java.util.Arrays;public class Main { public static void main(String[] args) { int[] arr = new int[]{1,3,3,4,5}; ArrayList<Integer> arr_new = new ArrayList<Integer>(); for (int i : arr) { arr_new.add(i); } arr_new.remove(3); System.out.println("Before deletion :" + Arrays.toString(arr)); System.out.println("After Deletion:" + arr_new); }}

A call to the remove(i) function removes the element at index i. Deletion in ArrayLists is relatively easier as compared to Arrays.

调用remove(i)函数将删除索引i处的元素。 与Arrays相比,ArrayLists中的删除相对容易。

结论 (Conclusion)

We saw some examples of deleting elements in an array using different methods. The difference between the deletion of an element in an Array and an ArrayList is clearly evident. If deletion is to be performed again and again then ArrayList should be used to benefit from its inbuilt functions. Same is the case with the addition of elements to an array. ArrayList due to its dynamic nature provides easier and less cumbersome ways to alter an array.

我们看到了一些使用不同方法删除数组中元素的示例。 删除Array中的元素和ArrayList之间的区别很明显。 如果要一次又一次地执行删除操作,则应使用ArrayList来受益于其内置功能。 将元素添加到数组的情况也是如此。 ArrayList由于其动态特性而提供了更轻松,更省力的方式来更改数组。

翻译自: https://www.journaldev.com/42212/java-remove-array-elements

java数组删除数组元素

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。