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

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

對于某些數組整數,我的 remove 方法有什么問題?

對于某些數組整數,我的 remove 方法有什么問題?

慕少森 2022-09-07 21:26:36
我必須創建一個方法,該方法將從數組中刪除特定值,并創建一個沒有該特定值的新數組。例如,如果我的數組是 (0,2,3,5,3),而我想刪除 3,則新數組應該是 (0,2,5)。由于某種原因,它僅適用于前兩位數字。import java.util.Scanner;public class removeDemo {    public static void main(String[] args) {        // TODO Auto-generated method stub        //array of numbers        int array[] = new int[] {0,1,2,3,4,5};        //invokes method and prints result        //System.out.println(remove(3,array));        remove(3,array);    }    //method remove that removes selected number from array    public static int[] remove(int v, int[] in) {        //count variable counts how many non-target numbers        int count = 0;        //for loop that checks if value at certain index is not equal to "v", the target number for removal        for(int k = 0; k < in.length; k++) {            //checks if certain number at certain index of array is not equal to v, or in this case, 3            if(in[k] != v) {                //counter                count++;            }    }        //new array that will stores values except "v"        int copy[] = new int[count];        //prints the length        System.out.println("array length: " + copy.length);        //for loop that checks if number not 3        for(int a = 1; a < in.length;) {        //  sets number at certain index of main array into new array            if(in[a] != 3){                copy[a] = in[a];                a++;                System.out.println(copy[0]);                System.out.println(copy[1]);                System.out.println(copy[2]);                System.out.println(copy[3]);            }            else if(in[a] == 3) {                copy[a] = in[a+1];            }        }        //returns new array        return copy;}}如前所述,我需要新數組來排除要刪除的目標編號。
查看完整描述

3 回答

?
慕萊塢森

TA貢獻1810條經驗 獲得超4個贊

您可以使用如下所示的一些代碼獲得相同的結果:


// Add to params all inputs to remove from array

List<Integer> params = new ArrayList<>();


// Use Integer class instead of int datatype

Integer array[] = new Integer[] {0,1,2,3,4,3};


// Convert array to List class

List<Integer> list = new ArrayList<>(Arrays.asList(array));


// Remove all matches

list.removeAll(params);


查看完整回答
反對 回復 2022-09-07
?
慕絲7291255

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

您需要兩個索引變量來進行復制:一個貫穿輸入數組(如原始代碼中所示),另一個跟蹤您在輸出數組中的位置(new 變量)。它們不能相互計算(它們在開始時是相同的,但可以明顯小于最后)abba


int b = 0;

for(int a = 0; a < in.length; a++) {

  if(in[a] != v) {

    copy[b] = in[a];

    b++;

  }

}


查看完整回答
反對 回復 2022-09-07
?
湖上湖

TA貢獻2003條經驗 獲得超2個贊

使用Java8及其流功能,您可以執行如下操作:


 public static void main(String[] args) {

 int[] array = {3236,47,34,34,73,46,3,64,473,4,4,346,4,63,644,4,6,4};

 int[] newArray = removeAllOccurencesOf(array, 4);

 System.out.println(Arrays.toString(newArray));

 }



 public static int[] removeAllOccurencesOf(int[] array, int numberToRemove)

 {

 //stream integers from array, filter the ones that correspond to number to remove, get what's left to new array

 int[] newArray = IntStream.of(array).filter(i->i!=numberToRemove).toArray();

 return newArray;

 }


查看完整回答
反對 回復 2022-09-07
  • 3 回答
  • 0 關注
  • 103 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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