我正在制作一个数据结构,我可以:
- 添加起始位置和结束位置对
- 可以获得配对测试列表
- 有间隙的间隔
例如加(0,2),(0,1),(3,4),(6,8) 它将返回 (0,4),(6,8) 检查(2,4) - 真 check(3,8) - false,因为 4 和 6 之间有间隙。
如何修改下面的代码,例如set list
和list data
将包含原始int
而不是对象Integer
?
编辑:我知道java泛型不能是基元,所以List是不可能的。我的动力来自于其他数据结构的实现。
例如 HashMap< Integer, Object> 基本上与 android SparseArray 相同,不同之处在于 SparseArray 使用 int 而不是 Integers 作为键。
public class GapAwareList {
Set<Integer> list = new HashSet<>();
public void put(int start, int end) {
for (int i = start; i <= end; i++) {
list.add(i);
}
}
public void remove(int start, int end) {
for (int i = start; i <= end; i++) {
list.remove(i);
}
}
public List<Pair<Integer, Integer>> getPairs() {
List<Integer> data = new ArrayList<>();
data.addAll(list);
Collections.sort(data);
List<Pair<Integer, Integer>> pairs = new ArrayList<>();
int last = data.get(data.size() - 1);
int s = -1;
int e;
for (int i = 0; i <= last; i++) {
if (list.contains(i)) {
if (s == -1) {
s = i;
}
e = i;
if (!list.contains(i + 1)) {
Pair<Integer, Integer> p = new Pair<>(s, e);
pairs.add(p);
s = -1;
}
}
}
return pairs;
}
public boolean haveGap(int start, int end) {
boolean b = false;
for (int i = start; i < end; i++) {
if (!list.contains(i)) {
b = true;
break;
}
}
return b;
}
}
public class Pair {
public final int first;
public final int second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
}
public class ExampleUnitTest {
@Test public void gapSize() throws Exception {
GapAwareList gaps = fillGaps();
gaps.put(0, 2);
gaps.put(0, 1);
gaps.put(3, 4);
gaps.put(6, 8);
List<Pair<Integer, Integer>> pairs = gaps.getPairs();
assertEquals(3, pairs.size());
}
@Test public void gapFirst() throws Exception {
GapAwareList gaps = fillGaps();
gaps.put(0, 2);
gaps.put(0, 1);
gaps.put(3, 4);
gaps.put(6, 8);
List<Pair<Integer, Integer>> pairs = gaps.getPairs();
assertTrue(pairs.get(0).first == 0);
assertTrue(pairs.get(0).second == 4);
}
@Test public void gapSecond() throws Exception {
GapAwareList gaps = fillGaps();
List<Pair<Integer, Integer>> pairs = gaps.getPairs();
assertTrue(pairs.get(1).first == 6);
assertTrue(pairs.get(1).second == 8);
}
private GapAwareList fillGaps() {
GapAwareList gaps = new GapAwareList();
//add (0,2),(0,1),(3,4),(6,8)
gaps.put(0, 2);
gaps.put(3, 4);
gaps.put(0, 1);
gaps.put(6, 8);
gaps.put(13,11);
gaps.put(13, 13);
gaps.put(13, 14);
return gaps;
}
@Test public void checkGaps(){
GapAwareList gaps = fillGaps();
assertFalse(gaps.haveGap(0,4));
assertFalse(gaps.haveGap(6, 8));
assertFalse(gaps.haveGap(6, 7));
assertTrue(gaps.haveGap(4, 6));
}
}
最佳答案
来自Java documentation "Restrictions on Generics" :
Cannot Instantiate Generic Types with Primitive Types
Consider the following parameterized type:
class Pair<K, V> { private K key; private V value; public Pair(K key, V value) { this.key = key; this.value = value; } // ... }
When creating a
Pair
object, you cannot substitute a primitive type for the type parameterK
orV
:Pair<int, char> p = new Pair<>(8, 'a'); // compile-time error
You can substitute only non-primitive types for the type parameters
K
andV
:Pair<Integer, Character> p = new Pair<>(8, 'a');
Note that the Java compiler autoboxes
8
toInteger.valueOf(8)
and'a'
toCharacter('a')
:Pair<Integer, Character> p = new Pair<>(Integer.valueOf(8), new Character('a'));
For more information on autoboxing, see Autoboxing and Unboxing in the Numbers and Strings lesson.
关于java - 如何用基元替换 Integer 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42110898/