Java 如何用基本体替换整数对象
我正在制作一个数据结构,我可以:Java 如何用基本体替换整数对象,java,Java,我正在制作一个数据结构,我可以: 添加具有起始和结束位置的配对 可以获取测试对的列表 间隔 例如,添加(0,2)、(0,1)、(3,4)、(6,8) 它将返回(0,4)、(6,8) 检查(2,4)-正确 检查(3,8)-错误,因为4和6之间存在间隙 如何修改下面的代码,例如setlist和listdata将包含原语int,而不是对象整数 编辑:我知道java泛型不能是原语,所以列表是不可能的。我的动机来自其他数据结构的实现。 e、 g HashMap与android SparseArray基
- 添加具有起始和结束位置的配对
- 可以获取测试对的列表
- 间隔
list
和listdata
将包含原语int
,而不是对象整数
编辑:我知道java泛型不能是原语,所以列表是不可能的。我的动机来自其他数据结构的实现。e、 g HashMap
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));
}
}
公共类GapAwareList{
Set list=新的HashSet();
公共作废put(整数开始,整数结束){
对于(int i=start;iFrom:
无法用基元类型实例化泛型类型
考虑以下参数化类型:
class Pair<K, V> {
private K key;
private V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
// ...
}
只能用非基本类型替换类型参数
K
和V
:
Pair<int, char> p = new Pair<>(8, 'a'); // compile-time error
Pair<Integer, Character> p = new Pair<>(8, 'a');
有关自动装箱的详细信息,请参见中的
教训
JAVA泛型不支持原语。如果你想这样做,你可以创建一个显式数组。你是在要求一个不允许间隙的列表实现,还是关于泛型?@TimBiegeleisen关于泛型。例如HashMap与android SparseArray基本相同,不同之处在于SparseArray使用了整数而不是整数代表keys@PriyanshGoel是的,我知道这一点,请看一下我的编辑,你考虑创建一个类,比如“代码>范围<代码>,它包含整数并有一个<代码>列表<代码>?嗯,如果你知道这一点,那么不要使用泛型。
Pair<Integer, Character> p = new Pair<>(Integer.valueOf(8), new Character('a'));