第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在不創(chuàng)建新數(shù)組或不使用 ArrayList 的情況下從數(shù)組中刪除元素?

如何在不創(chuàng)建新數(shù)組或不使用 ArrayList 的情況下從數(shù)組中刪除元素?

嗶嗶one 2023-10-13 16:36:59
我正在嘗試從對(duì)象數(shù)組中刪除一個(gè)對(duì)象,但是我似乎無(wú)法讓它正常工作。我是 java 新手,注意到你不能簡(jiǎn)單地對(duì)數(shù)組執(zhí)行 .remove() 。我知道你可以使用ArrayList,但是有沒(méi)有辦法從java中的數(shù)組中刪除元素?我知道從技術(shù)上講你無(wú)法做到這一點(diǎn),因?yàn)閿?shù)組是固定長(zhǎng)度的,所以我所做的是將值 null 分配給數(shù)組中的對(duì)象引用。這是可行的,但是當(dāng)我打印數(shù)組時(shí),我收到此錯(cuò)誤。錯(cuò)誤 java.lang.NullPointerException對(duì)象定義public class Dog {    //Iniatilising instance variables    private String name;    private String breed;    private Gender gender;    private int age;    ...測(cè)試類常量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);測(cè)試類刪除方法    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貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊

在java中,當(dāng)初始化數(shù)組時(shí),它會(huì)為初始化保留內(nèi)存,該內(nèi)存需要保存與聲明的數(shù)組的數(shù)據(jù)類型相對(duì)應(yīng)的數(shù)據(jù)。

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

您可以做的簡(jiǎn)單事情是通過(guò)覆蓋需要從數(shù)組中刪除的數(shù)組元素來(lái)向左移動(dòng)其他數(shù)組元素。這意味著聲明一個(gè)新變量來(lái)保存數(shù)組當(dāng)前保存的元素?cái)?shù)量。

那么刪除數(shù)組元素就變得簡(jiǎn)單了。

  • 從要?jiǎng)h除的元素開(kāi)始向左移動(dòng)元素。

  • 將變量減 1

    當(dāng)您想要獲取當(dāng)前數(shù)組元素時(shí),只需參考 no of elements 變量循環(huán)遍歷數(shù)組即可。


查看完整回答
反對(duì) 回復(fù) 2023-10-13
?
胡子哥哥

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超6個(gè)贊

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

盡管

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

您需要做的是創(chuàng)建一個(gè)新數(shù)組,并使用System.arrayCopy()或任何其他方式將該數(shù)組的剩余內(nèi)容復(fù)制到新數(shù)組中。事實(shí)上,您將使用的所有其他 API 和函數(shù)都會(huì)執(zhí)行此操作,但您無(wú)需重新發(fā)明輪子。

   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));


查看完整回答
反對(duì) 回復(fù) 2023-10-13
?
慕工程0101907

TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array中,告訴您可以使用 array.splice(pos, n) 按索引刪除項(xiàng)目,其中 pos 是開(kāi)始刪除的索引,n 是刪除的元素?cái)?shù)量。



查看完整回答
反對(duì) 回復(fù) 2023-10-13
  • 3 回答
  • 0 關(guān)注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)