亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在不創建新數組或不使用 ArrayList 的情況下從數組中刪除元素?

如何在不創建新數組或不使用 ArrayList 的情況下從數組中刪除元素?

嗶嗶one 2023-10-13 16:36:59
我正在嘗試從對象數組中刪除一個對象,但是我似乎無法讓它正常工作。我是 java 新手,注意到你不能簡單地對數組執行 .remove() 。我知道你可以使用ArrayList,但是有沒有辦法從java中的數組中刪除元素?我知道從技術上講你無法做到這一點,因為數組是固定長度的,所以我所做的是將值 null 分配給數組中的對象引用。這是可行的,但是當我打印數組時,我收到此錯誤。錯誤 java.lang.NullPointerException對象定義public class Dog {    //Iniatilising instance variables    private String name;    private String breed;    private Gender gender;    private int age;    ...測試類常量public class TestDog {//Create a constant value for MAXstatic final int MAX = 10;//Define a constant PROMPTstatic final String PROMPT = "---->";//Define a constant SPACESstatic final String SPACES = "     ";//String array of menu optionsstatic final String options[] = {"Add new Dog","Display details for a Dog",        "Update details for a Dog","List all Dogs","Delete all Dogs","Delete one Dog","Quit"};//Define a constant QUITstatic final int QUIT = options.length;//Create an array capable of managing up to MAX Dog instancesstatic Dog pets[] = new Dog[MAX];static Dog empty[] = new Dog[MAX];//Define a value 'count' to indicate number of objects in arraystatic int count = 0;//A menu titlestatic String title = "Dog Manager";//Define a menu using title & optionsstatic Menu myMenu = new Menu(title,options);//Define a Scannerstatic Scanner input = new Scanner(System.in);測試類刪除方法    public static void deleteOneDog() {    System.out.println("\nOK - Delete one dog");    boolean noDogs = false;    if (count == 0) {        System.out.println(PROMPT+"Sorry, there are no dogs.");        noDogs = true;        }
查看完整描述

3 回答

?
LEATH

TA貢獻1936條經驗 獲得超7個贊

在java中,當初始化數組時,它會為初始化保留內存,該內存需要保存與聲明的數組的數據類型相對應的數據。

所以刪除數組元素是你不能做的事情。

您可以做的簡單事情是通過覆蓋需要從數組中刪除的數組元素來向左移動其他數組元素。這意味著聲明一個新變量來保存數組當前保存的元素數量。

那么刪除數組元素就變得簡單了。

  • 從要刪除的元素開始向左移動元素。

  • 將變量減 1

    當您想要獲取當前數組元素時,只需參考 no of elements 變量循環遍歷數組即可。


查看完整回答
反對 回復 2023-10-13
?
胡子哥哥

TA貢獻1825條經驗 獲得超6個贊

在 Java 中沒有直接的方法從數組中刪除元素。雖然Java對象中的Array,但它不提供任何方法來搜索add()remove()搜索Array中的元素。ArrayList這就是像HashSet 這樣的 Collection 類非常流行的原因。

盡管

感謝Apache Commons Utils,您可以使用該類ArrayUtils比您自己操作更輕松地從數組中刪除元素。要記住的一件事是,Java 中的數組是固定大小的,一旦創建數組,就無法更改其大小,這意味著刪除或刪除項目不會減少數組的大小。事實上,這是 Java 中 Array 和 ArrayList 之間的主要區別。

您需要做的是創建一個新數組,并使用System.arrayCopy()或任何其他方式將該數組的剩余內容復制到新數組中。事實上,您將使用的所有其他 API 和函數都會執行此操作,但您無需重新發明輪子。

   import java.util.Arrays;

    import org.apache.commons.lang.ArrayUtils;


     ....

    //let's create an array for demonstration purpose

    int[] test = new int[] { 101, 102, 103, 104, 105};


    System.out.println("Original Array : size : "

                           + test.length );

    System.out.println("Contents : " + Arrays.toString(test));


    // let's remove or delete an element from an Array

    // using Apache Commons ArrayUtils

    test = ArrayUtils.remove(test, 2); //removing element at index 2


    // Size of an array must be 1 less than the original array

    // after deleting an element

    System.out.println("Size of the array after

              removing an element  : " + test.length);

    System.out.println("Content of Array after

             removing an object : " + Arrays.toString(test));


查看完整回答
反對 回復 2023-10-13
?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array中,告訴您可以使用 array.splice(pos, n) 按索引刪除項目,其中 pos 是開始刪除的索引,n 是刪除的元素數量。



查看完整回答
反對 回復 2023-10-13
  • 3 回答
  • 0 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號