2014-01-23 35 views
2

如果有人能指出我正確的方向,我將非常感謝。我正在通過以下Murach的Java書籍的在線示例學習Java。我想寫一個函數,它有兩個參數:搜索數組中的元素,然後返回一個帶索引的新數組

參數:

1: An int myArr 
2: An int num 

我想經過了「myArr,該」,並發現如果「民」中存在一次或一次以上「 myArr,該」。因此,讓我們說如果「myArr」有「num」可以說在三個地方,我想在函數內創建一個新的數組,並捕獲這個新數組中的所有三個索引,並返回新的數組。我已經有一段時間了,我覺得我很困惑,可以使用一些幫助。

我的問題是,我不確定如何在函數中聲明新的數組大小。所以,讓我們假設「myArr,該」有5個要素:

1,2,1,1,4 

,如果我在尋找1號,你可以看到,1號顯得比myArr,該內一次。所以現在我需要在函數中聲明一個大小爲3的新數組,然後將相關索引(即0,2和3)推送到這個新創建的數組中,然後返回它。我沒有做我的for循環,並最終與IndexOutOfBoundArray。我感謝你的時間和建議。

這裏是我的代碼:

public void searchArray(int[] myArr, int num){ 

    int counter = 0; 
    int[] newArr = new int[counter]; 

    //FOR-LOOP 
    for(int i=0; i < myArr.length; i++){ 
     if(myArr[i] == num){ 
      counter++;   
     }    
    } 
    // SECOND FOR LOOP 
    for(int j=0; j < myArr.length; j++){ 
     newArr[j] = j; 
    } 

    // PRING OUT 'newArr' elements 
    for(int value : newArr){ 

     System.out.printf(" %d ", value);  
    } 
} 

謝謝。

+1

ArrayList將解救你 –

+1

int [] newArr = new int [counter];這就是爲什麼你得到綁定異常的arrayindexout的原因; 當你拿到計數器時,將它活化。 –

回答

2

您的第一個錯誤是您在計算計數器之前創建newArr,因此它總是會創建一個包含0個元素的數組。

其他錯誤是你在正確

public void searchArray(int[] myArr, int num){ 

    int counter = 0; 

    //FOR-LOOP 
    for(int i=0; i < myArr.length; i++){ 
     if(myArr[i] == num){ 
      counter++; 
     } 
    } 

    int[] newArr = new int[counter]; 

    // SECOND FOR LOOP 

    for(int i=0, j=0; i < myArr.length; i++){ 
     if(myArr[i] == num){ 
      newArr[j++] = i; 
     } 
    } 

    // PRING OUT 'newArr' elements 
    for(int value : newArr){ 

     System.out.printf(" %d ", value); 
    } 
} 
+0

並返回新數組... :) – TheLostMind

+0

謝謝TheLostMind。我想,但這讓我想到如何插入從這個非常功能返回的數組。我是否需要聲明一個變量類型int [],然後將該函數賦值給該變量?但是我不能聲明變量類型數組而沒有指定大小,對吧? – Combustion007

+0

謝謝泰泰。我感謝你的幫助。現在感謝你,我知道我做錯了什麼。 – Combustion007

0

如上所述,您可以使用可變長度List,但您必須處理intInteger

我的建議:首先獲取元素的數量,然後創建一個很長的數組,然後用索引填充它。

public int[] searchArray(int[] myArr, int num) { 
    // get number of elements 
    int n = 0; 
    for(int i = 0; i < myArr.length; i++) { 
     if(myArr[i] == num) { 
      n++; 
     } 
    } 

    // create an array 
    int[] indices = new int[n]; 

    // populate the array with indices 
    n = 0; 
    for(int i = 0; i < myArr.length; i++) { 
     if(myArr[i] == num) { 
      indices[n++] = i; 
     } 
    } 
    return indices; 
} 
+0

謝謝你,保羅。我感謝你的幫助。 – Combustion007

1

更改您的密碼設置newArr值:

public int[] searchArray(int[] myArr, int num){ 

int counter=0;    //keep track of indices 
int[] newArr = new int[myArr.length]; // safer route... All numbers of the array could be same... 

//FOR-LOOP 
for(int i=0; i < myArr.length; i++){ 
    if(myArr[i] == num){  // if arr[i] has num 
     newArr[counter]=i;  // add index value to newArr 
     counter++ ;   // increment count .. so now newArr points to nextIndex 
    }    
} 


// PRING OUT 'newArr' elements 
for(int value : newArr){ 

    System.out.println(value);  
} 
return newArr; 

}

0

做這樣

public int[] searchArray(int[] myArr, int num){ 
    int counter = 0;  
    //Finding the new Array lenght here; 
    for(int i=0; i < myArr.length; i++){ 
     if(myArr[i] == num){ 
      counter++;   
     }    
    }  
    //Creating new array here 
    int[] newArr = new int[counter]; 
    // Storing the index in the Second array. 
    for(int i=0,j=0; i < myArr.length; i++){ 
     if(myArr[i] == num){ 
       newArr[j++] = i;     
     } 
    }   
    return newArr; 
} 

注意:使用Arr如果數組長度未知,則使用ayList而不是數組。

+0

謝謝你的代碼Parbhakaran,我感謝你的時間和幫助。 – Combustion007

+0

@ Combustion007歡迎您.. – Prabhakaran