Ryu
Ryu

Reputation: 13

Newbie having trouble with arrays

I have been studying Java by myself for a few weeks but I have become completely stuck in one challenge question I found online. I have thouroughly searched Google to see if anyone else asked it but I can't seem to find anything. Since I don't have any teachers, I was wondering if anyone on StackOverflow could help me out.

I am having an issue with one of my methods because it keeps failing my test cases and I'm not sure what exactly is making it go wrong.

Basically, if I input an array like [2, 8, 3, 9, 7], I want it to come out like [2, 3, 7, 8, 9]. However, with this poorly written method I have here, I keep getting the extra element at the beginning, which results in [2, 2, 3, 7, 8, 9]. My specific goal with this method is to find and return an array that contains all values greater/equal to "high" and less than/equal to "low" , including duplicates. The length of this array should be the same as the # of elements in the 'range', hence the method name.

I just wanted to be properly directed towards the next step that I should be taking to eliminate this first element issue. I know that the code is probably awful and messy, so please bear with me.

Thank you; all help is appreciated!

public static int[] range(int[] a, int low, int high) {
  if (a == null || a.length == 0) {
     throw new IllegalArgumentException();
  }

  int[] newArr = new int[0];
  if (low <= high) {
     if (a.length == 1 && low >= a[0] && high <= a[0]) {
        return a;
     }

     for (int i : a) {
        if (i >= low && i <= high) {
           int[] duplicate = new int[(a.length + 1)];
           int count = 0;
           for (int num : a) {
              duplicate[count] = num;
              count++;
           }
           duplicate[count] = i;
           return duplicate;
        }
     }
  }
  if (newArr.length > 1) {
     Arrays.sort(newArr);
  }
  return newArr;

}

Upvotes: 1

Views: 79

Answers (1)

Sci Prog
Sci Prog

Reputation: 2691

If I read your specification correctly, you take an input array a, and two values low and high. You have to return a new array with only the elements between (inclusively) low and high.

You did not write anywhere in which order the elements should (apart than in your test case where the elements are sorted in ascending order). Note: you can easily add the sort() method if you require it.

The main problem is that you have to determine the size of the returned array. There are many ways to do it. For simplicity, I'll do it in two loops.

public static int[] range( int[] a, int low, int high) {
  int result_size=0;
  int i, j;
  // determine result size
  for (i=0; i< a.length;i++) {
    if ((low <= a[i]) && (a[i] <= high)) result_size++;
  }
  // build the result array
  int [] result = new int[result_size];
  j = 0;   // destination
  for (i = 0; i < a.length; i++) {
    if ((low <= a[i]) && (a[i] <= high)) {
      result[j] = a[i];
      j++;
    }
  }
  return result;
}

Upvotes: 2

Related Questions