Java索引数组帮助。编码蝙蝠

Java索引数组帮助。编码蝙蝠,java,arrays,indexing,Java,Arrays,Indexing,我想解决的问题是: 给定任意长度的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

我想解决的问题是:

给定任意长度的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;


}
因为我得到了一个索引越界错误,我在尝试解决这个特定测试时遇到了困难。因为我不知道如何检查我的应答数组中是否已经有一个元素作为应答。长度始终为2,即使它在每个索引中没有指定的元素,因为它默认为零


感谢任何帮助,如果有人能在开始时改进我的两个if语句(它适用于较小的数字,但我知道当它达到较大的数字时,我无法编写这样的代码)。我想用ArrayList尽可能地回答这个问题。add(),但这个问题指定了一个数组,如果数组不是空的,它会指定一个数组来查找要预设的插槽数,这很烦人。

与其硬编码索引,不如使用一个变量并递增它

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;
}
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[]答案=新的int[answerindexs];
//索引变量
int i=0;
对于(int x=0;x 0&&x==0)
答案[i++]=a[0];
else如果(b.length>0&&x==1)
答案[i]=b[0];
}
返回答案;
}
导入java.util.array;
公共类试验台{
公共静态int[]进程(int[]a,int[]b){
int[][]数组=新的int[2][];
数组[0]=a;
数组[1]=b;
整数计数=0;
for(int i=0;i0){
计数++;
}
}
int-curIndex=0;
int[]结果=新的int[计数];
for(int i=0;i0){
结果[curIndex++]=数组[i][0];
}
}
返回结果;
}
/**
* 
*@param args
*/
公共静态void main(字符串[]args){
int[]a={1,2};
int[]b={3,4};
System.out.println(Arrays.toString(进程(a,b));
}
}

您实际上可以不使用循环解决此问题,只有通过if子句:

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;
}

关于如何设置列表中的索引数量的任何提示,或者我所做的是唯一的方法吗?用我能想到的设置索引数量的最短方法更新了我的答案。请给出一些上下文,说明为什么这可以解决问题。仅仅删除一堆od代码是很难理解的
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)));
    }
}
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;
}
    public int[] front11(int[] a, int[] b) {
  int[] arr=new int[0];
  int[] arr1=new int[1];
  int[] arr2=new int[2];

  if(a.length==0 && b.length==0){
  return arr;
}

    if(a.length>0 && b.length>0){
    arr2[0]=a[0];
    arr2[1]=b[0];
    return arr2;

 }

  if(a.length==0){
    arr1[0]=b[0];
    return arr1;
  }
  if(b.length==0){
    arr1[0]=a[0];
    return arr1;
  }




  return arr;
}