2012-03-22 105 views
3

這裏的佈局是什麼添加元素到數組的java

index num 
0  [10] 
1  [20] 
2  [30] 
(Add 35 here) 
3  [40] Move elements down 
4  [50] 
5  [60] 
6  [70] 

那麼我的方法是這樣的

public static void method(int[] num, int index, int addnum) 
{ 

} 

我怎麼能在裏面加35?

嘗試這樣:

public static void method(int[] num, int index, int addnum) 
{ 
int index = 10; 
for(int k = num.length k>3; k++) 
{ 
     Num[k]=num[k++] 
} 
    Num[3] = 35; 
+1

鮮爲人知的事實,你可以用Arrays.binarySearch找到你應該插入索引(http://docs.oracle.com/javase/6/docs/api/java/util/Arrays的.html)。 – 2012-03-22 13:57:55

+0

您正在嘗試使用正確的軌道,但在複製要添加的值後,需要追加原始數組的末尾。 – DukeOfMarmalade 2012-03-22 14:10:21

回答

1

您需要

  1. 分配與空間,一個新元素的新數組。

    int[] newArray = new int[oldArray.length + 1]; 
    
  2. 複製所有元素並留下插入空間。

    for (int i = 0; i < newArray.length - 1; i++) 
        newArray[i < insertIndex ? i : i + 1] = oldArray[i]; 
    
  3. 將35插入空位。

    newArray[insertIndex] = numberToInsert; 
    

注意它的可能在這樣的方法做:

public static void method(int[] num, int index, int addnum) 
       ^^^^ 

因爲你不能變化num長度。

需要分配一個新的陣列,這意味着需要回報新的數組:

public static int[] method(int[] num, int index, int addnum) 
       ^^^^^ 

,然後調用該方法是這樣的:

myArr = method(myArr, 3, 35); 
+1

手頭的方法是'void'類型 - 新的分配數組是否會被丟棄? – amit 2012-03-22 13:46:46

0

好,除非陣列中存在「額外空間」,否則您不能將所有元素[從index開始]移至右側一個元素,並將35 [num]添加到t他相關的地方。
[實際發生的是最後一個元素被丟棄]。

然而 - 一個更好的解決方案可能是使用一個ArrayList,並使用方法myArrayList.add(index,element)

0

因爲這很近似於功課,你需要知道的是,你不能動態地增加數組的大小是什麼。所以在你的功能:

public static void(int[] num, int index, int addnum) 
{ 
     int[] temp = new int[num.length *2]; 
     for(int i = 0; i < index; i++) 
      copy num[i] into temp[i] 
     insert addnum into temp[index] 
     fill temp with remaining num values 
} 

上面的這個僞代碼應該讓你開始。

0

你在找什麼是insertion sort

它是課堂作業,所以它取決於你找出正確的代碼。

0

非常好聽的,你想要做這樣的事情:

public static void(int[] num, int index, int addnum) 
{  
    // initialize new array with size of current array plus room for new element 
    int[] newArray = new int[num.length + 1]; 

    // loop until we reach point of insertion of new element 
    // copy the value from the same position in old array over to 
    // same position in new array 
    for(int i = 0; i < index; i++) 
    { 
     newArray[i] = num[i]; 
    } 
    i = i + 1; // move to position to insert new value 

    newArray[i] = addnum; // insert the value 

    // loop until you reach the length of the old array 
    while(i < num.length) 
    { 
     newArray[i] = num[i-1]; 
    } 

    // finally copy last value over 
    newArray[i + 1] = num[i]; 
} 
+0

誰,你的while循環似乎很久就完成了。 – christophe31 2017-03-02 13:36:04

5

因爲這是你應該做到自己,我將只提供方法來實現它,而不是代碼:

如果您將在位置index上設置該號碼,您將覆蓋之前的值。所以,你需要做的是一個位置移動的每一個元素對從index從數組的結尾:num[x]成爲num[x+1]

你會發現,你需要做到這一點按相反的順序,否則你將填補您的陣列的值爲num[index]

在這個過程中,你需要決定如何處理數組的最後一項(num[num.length - 1]):

  • 你可以只覆蓋它,丟棄值
  • 你可以從返回它的功能
  • ,如果它是你可以拋出一個異常非零
  • 您可以創建一個新的數組,它是1項採取比當前的陣列,而不是大,以保持所有值

在此之後,你有重複num[index]:價值存在於num[index+1],也因爲您已經移動它了。

現在可以在不覆蓋現有值的情況下將新值寫入所需的位置。

編輯

你在你的代碼的幾個錯誤:

  • 增量k,你需要減量它(k--,不k++
  • 你修改k再次在你的循環體中:在每個循環中更新兩次
  • 如果用k = num.length開始,你會嘗試在num[num.length + 1]寫的,這是不可能的
0

這個怎麼樣?

public class test { 
public static void main(String[] arg) throws IOException 
{ 
int[] myarray={1,2,3,5,6};//4 is missing we are going to add 4 
int[] temp_myarray=myarray;//take a temp array 
myarray=addElement(myarray,0);//increase length of myarray and add any value(I take 0) to the end 
for(int i=0;i<myarray.length;i++) 
{ if(i==3) //becaues I want to add the value 4 in 4th place 
     myarray[i]=4; 
    else if(i>3) 
     myarray[i]=temp_myarray[i-1]; 
    else 
     myarray[i]=temp_myarray[i]; 
} 
for(int i=0;i<myarray.length;i++) 
    System.out.print(myarray[i]);//Print new array 
} 

static int[] addElement(int[] arr, int elem) { 
    arr = Arrays.copyOf(arr, arr.length + 1); 
    arr[arr.length - 1] = elem; 
    return arr; 
} 
}