Reputation: 21
Complete the implementation of one public static methods
greaterThan()
Return type: int[]
Parameter list: one int[]
array
parameter list, and one int
parameter v
.
Action: Returns a new array whose element values are those values in int[]
array parameter list that are greater than int parameter v
.
Consider the following code segment
int[] array = { 7, -1, -4, 2, 1, 6, 1, -3, 2, 0, 2, -7, 2, 8 };
int[] g1 = Determine.greaterThan( array, 2 );
int[] g2 = Determine.greaterThan( array, 7 );
int[] g3 = Determine.greaterThan( array, 9 );
causes array variables
g1
to represent a 3-element array with element values 7
, 6
, and 8
g2
, and to represent a 1-element array with element value 8
g3
to represent a 0
-element array
This is what I have so far:
public class Determine {
// method greaterThan(): reutrns new int[] array whose element values are the ones
// in list greater than v
public static int[] greaterThan( int[] list, int v){
int n = list.length;
int[] x = new int[ n ];
for( int i = 0; i < n; i++ ){
int value = list[i];
if( value > v ){
x[i] = value;
}
}
return x;
}
}
But it gives me the following results:
greaterThan( [ 7 -1 -4 2 1 6 1 -3 2 0 2 -7 2 8 ], 2 ): [ 7 0 0 0 0 6 0 0 0 0 0 0 0 8 ]
greaterThan( [ 7 -1 -4 2 1 6 1 -3 2 0 2 -7 2 8 ], 7 ): [ 0 0 0 0 0 0 0 0 0 0 0 0 0 8 ]
greaterThan( [ 7 -1 -4 2 1 6 1 -3 2 0 2 -7 2 8 ], 9 ): [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
So I basically need to remove the 0
s to make the array that contains just the rest!
Upvotes: 2
Views: 99
Reputation: 9059
In Java 8, this can be easily done with filter()
:
static int[] greaterThan(int[] list, int v) {
return Arrays.stream(list).filter(e -> e > v).toArray();
}
This works by turning list
into a stream, then filtering the elements that are greater than v
, turning the stream into an array again and returning it.
If you can't use Java 8, or are not allowed to use streams, you can achieve this with Arrays.copyOf()
:
static int[] greaterThan(int[] list, int v) {
// Create an array with the same length as list
int[] greaterThanV = new int[list.length];
// Index to be used in by the greaterThanV array
int numGreater = 0;
for (int i = 0; i < list.length; i++) {
int value = list[i];
if (value > v) {
// Store the value and increment the numGreater index
greaterThanV[numGreater++] = value;
}
}
// Return an array containing the first numGreater elements of greaterThanV
return Arrays.copyOf(greaterThanV, numGreater);
}
The difference from this to your method is that this uses numGreater
as the index to the result array (greaterThanV
) and only increments it when an element is stored. This means that, if your call is equivalent to greaterThan([7 -1 -4 2 1 6 1 -3 2 0 2 -7 2 8], 2)
, instead of returning:
[7 0 0 0 0 6 0 0 0 0 0 0 0 8]
The greaterThanV
array will contain:
[7 6 8 0 0 0 0 0 0 0 0 0 0 0]
In the end, since we stored three values, numGreater
will be 3. So when we do:
Arrays.copyOf([7 6 8 0 0 0 0 0 0 0 0 0 0 0], 3)
We get the trimmed array as result:
[7 6 8]
Upvotes: 2
Reputation: 7844
Since you don't know the size of the primitive array before hand, convert to it later.
List<Integer> newList = new ArrayList<Integer>();
//Code to check and store the elements
for(int i = 0; i<list.length; i++) {
if(list[i] > n)
newList.add(list[i]);
}
return newList.toArray(new int[list.size()]);
Upvotes: 0
Reputation: 549
The issue you are having is that you are storing the number in the ith spot no matter what. When the input number is greater it moves onto the next index in the array, meaning that a 0 gets stored in that location.
The way I would go about fixing this is by creating a counter that gets incremented only after a number has been added to the output array.
public static int[] greaterThan( int[] list, int v){
int n = list.length;
int[] x = new int[ n ];
int counter = 0; // added this
for( int i = 0; i < n; i++ ){
int value = list[i];
if( value > v ){
x[counter] = value; // changed this
counter++; // make sure to increase the counter!
}
}
return x;
}
Upvotes: 1