java Java中指向整数的指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5144691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pointer to an Integer in Java
提问by bluehallu
I have a code like this
我有这样的代码
int a,b;
switch(whatever){
case 1:
lots_of_lines_dealing_with_variable_a;
case 2:
same_lines_but_dealing_with_variable_b;
}
I thought of doing:
我想过这样做:
int a,b;
pointer_to_int p;
switch(whatever){
case 1:
p=a;
case 2:
p=b;
}
lots_of_lines_dealing_with_pointer_p;
It would reduce the code to about half the lines, but Java doesn't allow pointers to integers. So, is there any way to approach this?
它会将代码减少到大约一半的行数,但 Java 不允许指向整数的指针。那么,有没有办法解决这个问题?
Edit: The homework is much bigger than just this method. I need to Create a class called "DoubleList" which contains two linked lists in a single Vector. The integers I talk about are the pointers to the start of the Lists, which I need to move to other positions of the Lists when adding or removing elements to the Lists
编辑:作业比这种方法大得多。我需要创建一个名为“DoubleList”的类,它在单个 Vector 中包含两个链表。我所说的整数是指向列表开头的指针,在列表中添加或删除元素时需要将其移动到列表的其他位置
采纳答案by Teo Klestrup R?ijezon
You could try using boxing.
你可以试试用拳击。
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;
回答by Joachim Sauer
Maybe I'm missing something, but looks easily solvable to me:
也许我错过了一些东西,但对我来说看起来很容易解决:
int a,b;
switch(whatever) {
case 1:
a = manipulateValue(a);
break;
case 2:
b = manipulateValue(b);
break;
}
int manipulateValue(int v) {
// lots of lines dealing with variable v
return v;
}
If you don't need to modify the variables, then you can leave out the return-value (just use void
) and the assignment.
如果您不需要修改变量,那么您可以省略返回值(只需使用void
)和赋值。
If nothing else needs to call the method, then it should be private
(it's a general principle: give as little access as possible, as much as necessary).
如果没有其他需要调用该方法,那么它应该是private
(这是一个一般原则:尽可能少地访问,尽可能多地)。
回答by unwind
Yes, it's called "a method":
是的,它被称为“一种方法”:
private int lots_of_lines_dealing_with_value(int x)
{
.
.
.
return new_value;
}
int a,b;
Switch(whatever){
case 1:
p=lots_of_lines_dealing_with_value(a);
break;
case 2:
p=lots_of_lines_dealing_with_value(b);
break;
}
回答by PhilDin
Why can't you just do:
你为什么不能这样做:
int a,b;
Switch(whatever){
case 1:
f(a);
break;
case 2:
f(b);
break;
default:
// ???
}
回答by Edwin Buck
Java doesn't have pointers, it has references.
Java 没有指针,它有引用。
A pointer is a variable that contains a memory address. Typically one dereferences the pointer to obtain the memory needed for some operation.
指针是包含内存地址的变量。通常,人们会取消引用指针以获得某些操作所需的内存。
A reference is an index into a memory management table. Typically the memory management table is protected from direct access. In Java's case the reference can't be manipulated manually, as any read of the variable will result in returning the referenced object.
引用是内存管理表的索引。通常,内存管理表受到保护,无法直接访问。在 Java 的情况下,不能手动操作引用,因为对变量的任何读取都将导致返回引用的对象。
This has a lot of implications, but is necessary for decent automatic garbage collection. Garbage collection sometimes involves moving objects in memory to create larger areas of free space (for needed objects that cannot fit into the current memory holes). If Java exposed a pointer, then after memory compaction, you might have the "old" address of the memory.
这有很多含义,但对于体面的自动垃圾收集是必要的。垃圾收集有时涉及移动内存中的对象以创建更大的可用空间区域(用于无法放入当前内存空洞的所需对象)。如果 Java 暴露了一个指针,那么在内存压缩之后,您可能拥有内存的“旧”地址。
By using references, your reference is guaranteed to stay the same, even if the actual location of memory moves around. Internal to the JVM is a reference to pointer table, but you will never get to see it from a running program; because, if you ever touched it, it would mess up the automatic memory management.
通过使用引用,即使内存的实际位置四处移动,您的引用也能保证保持不变。JVM 内部是对指针表的引用,但您永远无法从正在运行的程序中看到它;因为,如果你碰过它,它就会搞乱自动内存管理。
回答by David Weiser
Use the Integer class instead of an int.
使用 Integer 类而不是 int。
To give you an example using the code you have:
给你一个例子,使用你拥有的代码:
Integer a,b;
Integer p;
switch(whatever){
case 1:
p=a;
case 2:
p=b;
}
回答by Eric
Well, for this scenario:
好吧,对于这种情况:
boolean useA;
int a, b;
if(useA)
lots_of_lines_dealing_with_variable_a;
else
same_lines_but_dealing_with_variable_b;
you can use:
您可以使用:
boolean useA;
int a, b;
int value = useA ? a : b;
// lines_dealing_with_value
if(useA)
a = value;
else
b = value;
But really. Methods!
但真的。方法!