1

I'm trying to create an array of values that are greater than a certain threshold entered by the user, so I'm transferring one array of total seconds and keeping the values that are greater than the threshold. I can transfer the values I need appropriately, but I can't seem to print the results without it also printing the ending elements that contain no values.

Below is the code I've written:

public static int[] overGivenMinutes(int minThres, int arrayValues[]) {
    int[] greaterThanThres = new int[arrayValues.length];
    int j = 0;
    for (int i = 0; i < arrayValues.length; i++) {
        int minutes = arrayValues[i] / 60;
        if (minutes > minThres) {
            greaterThanThres[j++] = arrayValues[i];
        }
    }
    return greaterThanThres;
}

When the user enters the 30 min mark, it prints:

Time[00] = 56:38
Time[01] = 34:27
Time[02] = 51:04
Time[03] = 40:07
Time[04] = 43:34
Time[05] = 00:00
Time[06] = 00:00
Time[07] = 00:00
Time[08] = 00:00
Time[09] = 00:00

I'm trying to figure out what I need to do to remove the elements 5-9 since they are empty, and I cannot figure out how to. Any help is very much appreciated.

1
  • 2
    Does the return have to be an array? or is a list acceptable Mar 24, 2023 at 16:49

3 Answers 3

3

Bill Mair's answer has the right idea - you need to return a smaller array without "dangling" elements. However, it may be easier to achieve this by streaming and filtering the array and then collecting it to an array than by creating a new array and then returning a partial copy of it:

public static int[] overGivenMinutes(int minThres, int[] arrayValues){
    return Arrays.stream(arrayValues).filter(i -> i / 60 > minThres).toArray();
}
2
  • I didn't want to go into the collections streaming API as it appears that the OP is still learning the ropes. I could also have "reused" the input array but that would have been destructive to the input data.
    – Bill Mair
    Mar 24, 2023 at 16:48
  • Yes I totally am just getting the ropes. I appreciate both of y'all and your inputs because both were very helpful. Mar 25, 2023 at 0:19
2
public static int[] overGivenMinutes(int minThres, int arrayValues[]){
    int tmp[] = new int[arrayValues.length];
    int j = 0;
    for (int i = 0; i < arrayValues.length; i++){
        int minutes = arrayValues[i] / 60;
        if (minutes > minThres){
            tmp[j++] = arrayValues[i];
        }
    }
    int greaterThanThres[] = new int[j];
    System.arrayCopy(tmp,0,greaterThanThres,0,j);
    return greaterThanThres;
}
2

You are defining the array length at the start. So you are left with empty values. What you can do is use an implementation of java.util.List such as ArrayList.

ArrayList<Integer> greaterThanThres = new ArrayList<Integer>();

When you want to add an element, just push it to the list.

greaterThanThres.add(arrayValues[i]);

This is one alternative. Or you can simply loop through your array again in the end and clean it up according to your condition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.