我有这样的代码

int a,b;
switch(whatever){
    case 1:
      lots_of_lines_dealing_with_variable_a;
    case 2:
      same_lines_but_dealing_with_variable_b;
}

我想到了:
int a,b;
pointer_to_int p;
switch(whatever){
    case 1:
      p=a;
    case 2:
      p=b;
}
lots_of_lines_dealing_with_pointer_p;

它将代码减少到大约一半行,但是Java不允许指针指向整数。那么,有什么办法可以解决这个问题?

编辑:家庭作业比仅此方法大得多。我需要创建一个名为“DoubleList”的类,该类在一个Vector中包含两个链接列表。我所讨论的整数是指向列表开头的指针,在向列表中添加或删除元素时,我需要将其移至列表的其他位置

最佳答案

您可以尝试使用拳击。

public class IntBoxer {
    public IntBoxer() {
    }
    public IntBoxer(int startValue) {
        this.value = startValue;
    }
    public int value;
}
IntBoxer a = new IntBoxer();
IntBoxer b = new IntBoxer();
IntBoxer p;
Switch(whatever){
    case 1:
      p=a;
      break;
    case 2:
      p=b;
      break;
}
lots_of_lines_dealing_with_pointer_p.value;

09-11 02:27