What's new

Java [SOLVED]How to remove the zeros

Status
Not open for further replies.

Phrog

Forum Veteran
Elite
Joined
Jan 25, 2017
Posts
2,129
Reaction
1,379
Points
636
I have here a code that needs to pass all the value >= min(4)
it does the work but it also prints the empty indices.
Is there a way to remove the 0s?
this is the code, it is in Processing Java:
Java:
void setup() {
  noLoop();
}
void draw() {
  int[] test = {3, 9, -2, 6, 1, 8};
  int[] result = filter(test, 4);
  println(result);
}

int[] filter(int[] origArr, int min){
  int[] newArr = new int[origArr.length];
  for (int i=0; i<newArr.length; i++){
    if (origArr[i]>=min)
     newArr[i] = origArr[i];
  }
  return newArr;
}
Code:
/*OUTPUT*/
[0] 0
[1] 9
[2] 0
[3] 6
[4] 0
[5] 8
naisip ko kaya ganun, gawa po ng return statement, it returns the original size of the orray that's why
i think it also prints all the empty or 0 element inside the new array
 
I'm still learning Java pero tried this and it worked.
Java:
int[] filter(int[] origArr, int min) {
    int[] newArr = new int[origArr.length];
    for (int i = 0; i < newArr.length; i++) {
        if (origArr[i] >= min) {
            newArr[i] = origArr[i];
        }
    }
    
    //hahanapin ang zeroes then aalisin sa list
    int targetIndex = 0;
    for (int j = 0; j < newArr.length; j++) {
        if (newArr[j] != 0)
            newArr[targetIndex++] = newArr[j];
    }

    int[] newArray = new int[targetIndex];
    System.arraycopy(newArr, 0, newArray, 0, targetIndex);
    return newArray;
}

// newArray = {9, 6, 8};
 
Gawa ka nalang ng counter variable inside sa filter method mo to determine the number of int na more than sa min na input.

Java:
import java.util.Arrays;

void setup()
{
  noLoop();
}
void draw()
{
  int[] test = {3, 9, -2, 6, 1, 8};
  int[] result = filter(test, 4);
  println(result);
}

int[] filter(int[] origArr, int min)
{
  int counter = 0;
  int[] newArr = new int[origArr.length];
 
  for (int i = 0; i < origArr.length; i++)
  {
    if (origArr[i] >= min)
    {
      newArr[counter] = origArr[i];
      counter++;
    }
    
  }
  return Arrays.copyOf(newArr, counter);
}
 
I'm still learning Java pero tried this and it worked.
Java:
int[] filter(int[] origArr, int min) {
    int[] newArr = new int[origArr.length];
    for (int i = 0; i < newArr.length; i++) {
        if (origArr[i] >= min) {
            newArr[i] = origArr[i];
        }
    }
   
    //hahanapin ang zeroes then aalisin sa list
    int targetIndex = 0;
    for (int j = 0; j < newArr.length; j++) {
        if (newArr[j] != 0)
            newArr[targetIndex++] = newArr[j];
    }

    int[] newArray = new int[targetIndex];
    System.arraycopy(newArr, 0, newArray, 0, targetIndex);
    return newArray;
}

// newArray = {9, 6, 8};
Maraming salamat po

Gawa ka nalang ng counter variable inside sa filter method mo to determine the number of int na more than sa min na input.

Java:
import java.util.Arrays;

void setup()
{
  noLoop();
}
void draw()
{
  int[] test = {3, 9, -2, 6, 1, 8};
  int[] result = filter(test, 4);
  println(result);
}

int[] filter(int[] origArr, int min)
{
  int counter = 0;
  int[] newArr = new int[origArr.length];
 
  for (int i = 0; i < origArr.length; i++)
  {
    if (origArr[i] >= min)
    {
      newArr[counter] = origArr[i];
      counter++;
    }
   
  }
  return Arrays.copyOf(newArr, counter);
}
maraming salamat po, working na po
 
Status
Not open for further replies.

Similar threads

Back
Top