NewtoJava
NewtoJava

Reputation: 49

Java Indexing array help. CodingBat

The question I am trying to solve is: http://codingbat.com/prob/p128270

Given 2 int arrays, a and b, of any length, return a new array with the first element of each array. If either array is length 0, ignore that array.

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

My Code so far:

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;


}

This question I have been trying to do by myself is fully stressing me out because every attempt I try to do java doesn't work the way I thought it did. The only test I did not pass is;

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

Because I get an index out of bound error, and I am having trouble trying to solve for this specific test. As I am not sure how to check if my answer array has an element already in it as answer.length is always 2 eventhough it has no assigned element in each index as it is defaulted to zero.

Any help appreciated, also if anyone could improve my two if statements at the beginning (It works for little numbers but I know when it gets to larger numbers I can't write code like this). I wanted to answer this question using ArrayList as I can just .add(), but this question specifies an array which is annoying to find out how many slots to preset.

Upvotes: 2

Views: 504

Answers (4)

Rares
Rares

Reputation: 1

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

Upvotes: 0

hamena314
hamena314

Reputation: 3109

You can actually solve this problem without using loops, only by if-clauses:

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

Upvotes: 0

user3068350
user3068350

Reputation: 226

Instead of hardcoding the indexes, use a variable and increment it if the array is not empty.

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

Upvotes: 1

Alex Suo
Alex Suo

Reputation: 3119

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

Upvotes: 0

Related Questions