2015-12-18 82 views
2

我試圖解決的問題是:http://codingbat.com/prob/p128270Java索引數組幫助。 CodingBat

鑑於2個INT陣列,a和b,任何長度的,返回一個新的數組,每個數組的第一個元素。如果任一數組的長度爲0,則忽略該數組。

front11({1, 2, 3}, {7, 9, 8}) → {1, 7} 
front11({1}, {2}) → {1, 2} 
front11({1, 7}, {}) → {1} 

到目前爲止我的代碼:

public int[] front11(int[] a, int[] b) { 
    int answerindexs = 2; 

    if(a.length == 0 || b.length == 0) 
     answerindexs = 1; 

    if(a.length == 0 && b.length == 0) 
     answerindexs = 0; 

    int[] answer = new int[answerindexs]; 

    for (int x = 0; x <= 1; x++){ 
     if(a.length > 0 && x == 0) 
      answer[0] = a[0]; 
     else if(b.length > 0 && x == 1) 
      answer[1] = b[0]; 
    } 
    return answer; 


} 

這個問題我一直在嘗試自己做完全強調我出去,因爲每次嘗試我嘗試做Java不工作,我的思維方式它做了。 我沒有通過的唯一測試是;

front11({}, {2, 8}) → {2} 

因爲我得到一個索引超出限制的錯誤,並且我無法解決這個特定的測試。由於我不確定如何檢查我的答案數組是否已經有一個元素,因爲answer.length始終爲2,即使它在每個索引中都沒有賦值元素,因爲它默認爲零。

任何幫助表示讚賞,如果任何人都可以改善我的兩個if語句在開始(它適用於小數字,但我知道什麼時候它得到更大的數字,我不能寫這樣的代碼)。我想用ArrayList來回答這個問題,因爲我可以.add(),但是這個問題指定了一個數組,這個數組很煩人地想知道要預置多少個插槽。

回答

1

不是對索引進行硬編碼,而是使用一個變量並在數組不爲空時增加它。

public int[] front11(int[] a, int[] b) { 

    int answerindexs = 0; 
    answerindexs = a.length > 0 ? answerindexs + 1 : answerindexs; 
    answerindexs = b.length > 0 ? answerindexs + 1 : answerindexs; 

    int[] answer = new int[answerindexs]; 
    //Index variable 
    int i = 0; 

    for (int x = 0; x <= 1; x++){ 
     if(a.length > 0 && x == 0) 
      answer[i++] = a[0]; 
     else if(b.length > 0 && x == 1) 
      answer[i] = b[0]; 
    } 
    return answer; 
} 
+0

幹得漂亮,謝謝:d – NewtoJava

+0

如何設置指標量的任何提示在我的名單上,還是我唯一的方式? – NewtoJava

+0

用最短的方式來設置我能想到的索引量,從而更新了我的答案。 – user3068350

0
import java.util.Arrays; 

public class TestBed { 

    public static int[] process(int[] a, int[] b) { 
     int[][] arrays = new int[2][]; 

     arrays[0] = a; 
     arrays[1] = b; 

     int count = 0; 
     for (int i = 0; i < arrays.length; i++) { 
      if (arrays[i].length > 0) { 
       count++; 
      } 
     } 

     int curIndex = 0; 
     int[] result = new int[count]; 

     for (int i = 0; i < arrays.length; i++) { 
      if (arrays[i].length > 0) { 
       result[curIndex++] = arrays[i][0]; 
      } 
     } 

     return result; 
    } 

    /** 
    * 
    * @param args 
    */ 
    public static void main(String[] args) { 
     int[] a = {1, 2}; 
     int[] b = {3, 4}; 

     System.out.println(Arrays.toString(process(a, b))); 
    } 
} 
0

實際上,你可以解決這個問題,而使用循環,只有當從句:

public int[] front11(int[] a, int[] b) { 
    // If both arrays are empty, we return the empty(!) array a and are done 
    if(a.length == 0 && b.length == 0){ 
    return a; 
    } 
    // If array a is empty, we create a new array with the first value of array b 
    if(a.length == 0){ 
    int[] result = {b[0]}; 
    return result; 
    } 
    // The same for array b 
    if(b.length == 0){ 
    int[] result = {a[0]}; 
    return result; 
    } 
    // At this point we know, that both arrays are not length 0, 
    // so we create a new array and put the first value from a and the first from b in it 
    int[] result = {a[0], b[0]}; 
    return result; 
}