2015-12-18 45 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; 
}