我有一段时间遇到了麻烦,所以我想在这里问一下。基本上,我需要将给定最小和最大索引的数组值“裁剪”到一些目标数组中,并且具有所需的最小和最大索引(最小和最大索引围绕0),并且数组大小尊重最小值和最大值之间的差异。实际数组当然从索引0开始,但是实际数据的偏移量可能不同。
我试过了(见下文),但是我遇到了一些困难。我的数学非常差。代码是为了方便运行而组织成JUnit测试,并且您还可以看到预期结果。我认为算法中区别区域差异的机制不好--必须有更通用的解决方案,其中可以针对所有情况使用相同的线条。类似于此类的东西。
这不是作业或任何其他事情,而是用于裁剪对象网格,以便可以动态缩小和放大网格。这只是第一步。
我哪里错了?
编辑:我已经用以下代码使其工作,但是否有任何可以合并的改进子情况,以使代码更小?我不喜欢它们的外观。此外,我正在使用Object [],但如果Integer []不能正常工作,请随意将其转换回int []进行测试。
我试过了(见下文),但是我遇到了一些困难。我的数学非常差。代码是为了方便运行而组织成JUnit测试,并且您还可以看到预期结果。我认为算法中区别区域差异的机制不好--必须有更通用的解决方案,其中可以针对所有情况使用相同的线条。类似于此类的东西。
这不是作业或任何其他事情,而是用于裁剪对象网格,以便可以动态缩小和放大网格。这只是第一步。
我哪里错了?
import static org.junit.Assert.*;
import java.util.Arrays;
import org.junit.Test;
public class Hmm {
@Test
public void shrinkTest1() {
int[] res = arrMod(new int[] { 3, 4, 5, 6, 7, 8 }, -2, 3, -1, 2);
int[] exp = new int[] { 4, 5, 6, 7 };
assertArrayEquals("Array " + Arrays.toString(res) + " not equal to expected " + Arrays.toString(exp), exp, res);
}
@Test
public void expandTest1() {
int[] res = arrMod(new int[] { 3, 4, 5, 6, 7, 8 }, -2, 3, -3, 4);
int[] exp = new int[] { 0, 3, 4, 5, 6, 7, 8, 0 };
assertArrayEquals("Array " + Arrays.toString(res) + " not equal to expected " + Arrays.toString(exp), exp, res);
}
@Test
public void expandTest2() {
int[] res = arrMod(new int[] { 3, 4, 5, 6, 7, 8 }, -2, 3, -3, 6);
int[] exp = new int[] { 0, 3, 4, 5, 6, 7, 8, 0, 0, 0 };
assertArrayEquals("Array " + Arrays.toString(res) + " not equal to expected " + Arrays.toString(exp), exp, res);
}
@Test
public void sameTest1() {
int[] res = arrMod(new int[] { 3, 4, 5, 6, 7, 8 }, -2, 3, -3, 2);
int[] exp = new int[] { 0, 3, 4, 5, 6, 7 };
assertArrayEquals("Array " + Arrays.toString(res) + " not equal to expected " + Arrays.toString(exp), exp, res);
}
public int[] arrMod(int[] data, int min, int max, int newmin, int newmax) {
int minDiff = newmin - min;
int maxDiff = newmax - max;
System.out.println("minDiff: " + minDiff + ", maxDiff: " + maxDiff);
int[] newdata = new int[newmax - newmin + 1];
if ((newmax - newmin) > (max - min)) {
System.arraycopy(data, 0, newdata, maxDiff, max - min + 1);
} else if ((newmax - newmin) < (max - min)) {
System.arraycopy(data, minDiff, newdata, 0, newmax - newmin + 1);
} else {
// ...
}
return newdata;
}
编辑:我已经用以下代码使其工作,但是否有任何可以合并的改进子情况,以使代码更小?我不喜欢它们的外观。此外,我正在使用Object [],但如果Integer []不能正常工作,请随意将其转换回int []进行测试。
public static final <T> T[] arrMod(T[] data, int min, int max, int newmin, int newmax) {
//System.out.println(
// "arrMod(data=" + Arrays.toString(data) + ",min=" + min + ",max=" + max +
// ",newmin=" + newmin + ",newmax=" + newmax + ")"
//);
int minDiff = newmin - min;
int maxDiff = newmax - max;
//System.out.println("minDiff: " + minDiff + ", maxDiff: " + maxDiff);
@SuppressWarnings("unchecked")
T[] newdata = (T[])Array.newInstance(data.getClass().getComponentType(), newmax - newmin + 1);
System.out.println("newdata: " + newdata);
if ((maxDiff - minDiff) > 0) {
// grow
//System.out.println("expand: (maxDiff - minDiff) > 0");
arraycopy(data, 0, newdata, -minDiff, max - min + 1);
} else if ((maxDiff - minDiff) < 0) {
// shrink
//System.out.println("shrink: (maxDiff - minDiff) < 0");
arraycopy(data, minDiff, newdata, 0, newmax - newmin + 1);
} else {
// move
//System.out.println("same: (maxDiff - minDiff) == 0");
if (min > newmin) {
arraycopy(data, 0, newdata, -minDiff, max - min + maxDiff + 1);
} else {
arraycopy(data, maxDiff, newdata, 0, max - min - maxDiff + 1);
}
}
return newdata;
}
编辑2: 改进测试用例:
import static org.junit.Assert.*;
import java.util.Arrays;
import org.junit.Test;
public class Hmm {
@Test
public void shrinkTest1() {
System.out.println();
System.out.println("======= SHRINK TEST 1 ========");
Integer[] res = WFMap.arrMod(new Integer[] { 3, 4, 5, 6, 7, 8 }, -2, 3, -1, 2);
Integer[] exp = new Integer[] { 4, 5, 6, 7 };
assertArrayEquals("Array " + Arrays.toString(res) + " not equal to expected " + Arrays.toString(exp), exp, res);
}
@Test
public void shrinkTest2() {
System.out.println();
System.out.println("======= SHRINK TEST 2 ========");
Integer[] res = WFMap.arrMod(new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, -5, 4, -1, 2);
Integer[] exp = new Integer[] { 5, 6, 7, 8 };
assertArrayEquals("Array " + Arrays.toString(res) + " not equal to expected " + Arrays.toString(exp), exp, res);
}
@Test
public void expandTest1() {
System.out.println();
System.out.println("======= EXPAND TEST 1 ========");
Integer[] res = WFMap.arrMod(new Integer[] { 3, 4, 5, 6, 7, 8 }, -2, 3, -3, 4);
Integer[] exp = new Integer[] { null, 3, 4, 5, 6, 7, 8, null };
assertArrayEquals("Array " + Arrays.toString(res) + " not equal to expected " + Arrays.toString(exp), exp, res);
}
@Test
public void expandTest2() {
System.out.println();
System.out.println("======= EXPAND TEST 2 ========");
Integer[] res = WFMap.arrMod(new Integer[] { 3, 4, 5, 6, 7, 8 }, -2, 3, -3, 6);
Integer[] exp = new Integer[] { null, 3, 4, 5, 6, 7, 8, null, null, null };
assertArrayEquals("Array " + Arrays.toString(res) + " not equal to expected " + Arrays.toString(exp), exp, res);
}
@Test
public void sameTest1() {
System.out.println();
System.out.println("======= SAME TEST 1 ========");
Integer[] res = WFMap.arrMod(new Integer[] { 3, 4, 5, 6, 7, 8 }, -2, 3, -3, 2);
Integer[] exp = new Integer[] { null, 3, 4, 5, 6, 7 };
assertArrayEquals("Array " + Arrays.toString(res) + " not equal to expected " + Arrays.toString(exp), exp, res);
}
@Test
public void sameTest2() {
System.out.println();
System.out.println("======= SAME TEST 2 ========");
Integer[] res = WFMap.arrMod(new Integer[] { 3, 4, 5, 6, 7, 8 }, -2, 3, -1, 4);
Integer[] exp = new Integer[] { 4, 5, 6, 7, 8, null };
assertArrayEquals("Array " + Arrays.toString(res) + " not equal to expected " + Arrays.toString(exp), exp, res);
}
@Test
public void sameTest3() {
System.out.println();
System.out.println("======= SAME TEST 3 ========");
Integer[] res = WFMap.arrMod(new Integer[] { 3, 4, 5, 6, 7, 8 }, -2, 3, -4, 1);
Integer[] exp = new Integer[] { null, null, 3, 4, 5, 6 };
assertArrayEquals("Array " + Arrays.toString(res) + " not equal to expected " + Arrays.toString(exp), exp, res);
}