Java 方法,该方法返回大于数组中选定整数的整数
这是我到目前为止所做的,它适用于一些测试用例。该方法适用于某些数组,但不适用于其他数组。我不知道问题在哪里。方法说明见上述方法注释Java 方法,该方法返回大于数组中选定整数的整数,java,arrays,search,methods,Java,Arrays,Search,Methods,这是我到目前为止所做的,它适用于一些测试用例。该方法适用于某些数组,但不适用于其他数组。我不知道问题在哪里。方法说明见上述方法注释 /** * Return an array of all the elements of a that are greater than val. * If a contains no elements greater than val, this method returns an * array of zero length. *
/**
* Return an array of all the elements of a that are greater than val.
* If a contains no elements greater than val, this method returns an
* array of zero length.
*
* @param a the array to be searched
* @param val the reference value
* @return the elements a[i] such that a[i] > val
*
*/
public static int[] greater(int[] a, int val) {
if (a == null || a.length == 0) {
throw new IllegalArgumentException();
}
int x = 0;
int[] copy = Arrays.copyOf(a, a.length);
Arrays.sort(copy);
int nearest = copy[0];
for (int i = 0; i < copy.length; i++) {
if (Math.abs(nearest - val) > Math.abs(copy[i] - val)) {
nearest = copy[i];
x = i;
}
}
if ((x + 1) >= copy.length) {
int[] badAnswer = new int[0];
return badAnswer;
}
else {
int[] answer = new int[(copy.length - 1) - x];
int index = 0;
while (index < answer.length) {
answer[index] = copy[x + (index + 1)];
index++;
}
return answer;
}
}
但这一次不行:
int z[] = {-5,-2,0,4,8,15,50};
@Test public void greaterTest2() {
int d[] = Selector.greater(z, -99);
int p[] = {-5,-2,0,4,8,15,50};
Assert.assertArrayEquals(d, p);
也不适用于所有小于val的重复整数:
int z[] = {0, 0, 0, 0, 0};
@Test public void greaterTest2() {
int d[] = Selector.greater(z, 51);
int p[] = {};
Assert.assertArrayEquals(d, p);
}
关于如何在我的方法中弥补这些差距,有什么想法吗?公共静态int[]更大(int[]a,int val)
public static int[] greater(int[] a, int val)
{
int[] greater = new int[a.length];
int greaterNumber = 0;
for (int i = 0; i < greater.length; i++)
if (a[i] > val)
greater[greaterNumber++] = a[i];
return Arrays.copyOf(greater, greaterNumber);
}
{
int[]更大=新int[a.长度];
int greaterNumber=0;
for(int i=0;ival)
更大的[greaterNumber++]=a[i];
返回数组.copyOf(越大,数目越大);
}
我会使用JDK,只需要两行代码:
public static int[] greater(int[] a, int val) {
Arrays.sort(a);
return Arrays.copyOfRange(a, Math.abs(Arrays.binarySearch(a, val) + 1), a.length;
}
我省略了参数检查等,以强调这种方法的优雅和简单
看到一个关于ideone的答案。我看到我不是第一个答案,但我的答案被注释为:
private static int[] greater(int[] array, int v) {
// create space for the potential values greater than 'v'
int[] potentials = new int[array.length];
// an 'insertion point' in to the potentials array.
int ip = 0;
for (int a : array) {
// for each value in the input array....
if (a > v) {
// if it is greater than 'v', add it to the potentials
// and increment the ip insertion point.
potentials[ip++] = a;
}
}
// return the valid values from the potentials
return Arrays.copyOf(potentials, ip);
}
你的算法似乎太复杂了:一旦数组被排序,循环直到你找到一个item>val,然后arraycopy这个索引处或之后的任何东西……我对java非常陌生,我们正在为它上课。你有什么方法可以详细说明你的方法吗?@assylias是正确的,他说的不依赖java吗。。当您对数组进行排序后,只需循环已排序的数组,然后返回所有值,直到项目>valI相信我现在理解您。在没有更大价值的情况下,我如何破例?
private static int[] greater(int[] array, int v) {
// create space for the potential values greater than 'v'
int[] potentials = new int[array.length];
// an 'insertion point' in to the potentials array.
int ip = 0;
for (int a : array) {
// for each value in the input array....
if (a > v) {
// if it is greater than 'v', add it to the potentials
// and increment the ip insertion point.
potentials[ip++] = a;
}
}
// return the valid values from the potentials
return Arrays.copyOf(potentials, ip);
}