我有一些Stack的pop方法的代码,我试图找出它如何避免游荡,同时仍然返回我们的索引当前指向的元素:
public String pop()
{ // Remove item from top of stack.
String item = a[--N];
a[N] = null; // Avoid loitering (see text).
if (N > 0 && N == a.length/4) resize(a.length/2);
return item;
}
Run Code Online (Sandbox Code Playgroud)
根据我的理解,我们将String对象的引用项指向我们的数组a的索引元素(我们从最后一个元素开始,使用它的当前大小N-1,因此减少).然后,如果我们返回引用,为什么我们在这样做之前设置我们的引用指向null的索引元素?是不是这个项目指向什么都没有,什么也没有返回?
虽然我已经编写了C,C++和C#多年,但我只是表面上熟悉Java.通过Java学院项目帮助我的Comp Sci子,他需要从Java中的方法返回对两个对象的引用.我建议将一个作为函数值返回,将第二个作为参考.他不知道该怎么做.我做了一点研究,意识到这可能是不可能的.我的问题是在Java中,当一个方法需要返回一个对象的多个引用时,常用的方法是什么.这是我儿子案例中的具体例子.
// This method returns references to the head and tail objects from the passed in
// linked list. The head object is returned as the function value and the tail is
// returned as a parameter.
public Static Node GetHeadTail(List list, Node tail)
Run Code Online (Sandbox Code Playgroud)
我意识到上面的代码在Java中不起作用,因为tail是对节点的引用,而在Java中引用本身是通过值传递的.在Java中处理这个问题最常见的方法是什么?我儿子的解决方案是为函数值返回一个2节点对象的数组.我说这是一个糟糕的解决方案,因为它没有记录数组中每个元素的含义.另一种解决方案是创建一个包含头部和尾部引用的对象.然而,在特定示例中,头指针是最感兴趣的,并且如果返回对象,则如果他们想要的只是头部,则会为该方法的调用者创建不期望的编码开销.
是否可以让我的应用程序在运行时更新配置设置?我可以轻松地在我的UI中公开我想要的设置,但有没有办法允许用户更新设置并使它们永久保存,即将它们保存到config.yaml文件中?我可以看到它手动更新文件的唯一方法然后重启服务器似乎有点限制.
如果一个方法填充/修改一个对象,是否最好返回该对象或将返回类型保持为void,该方法是否会通过其引用修改Object?
public Obj populate(Obj o)
{
....
return o;
}
public void populate(Obj o)
{
....
}
Run Code Online (Sandbox Code Playgroud)
我知道这是一个微不足道的问题,但哪一个最受欢迎?
我阅读了很多文章,并且都说Java是值得传递的.但我仍然没有解释传递值和参考值之间的区别.我写了一个示例程序,它像这样执行..
public class PassByValue {
private int a;
private int b;
public PassByValue(int a, int b) {
this.a = a;
this.b = b;
}
public static int mul(int a, int b) {
int z = a * b;
System.out.println("The Value of Z inside mul: " + z);
return z;
}
public static void main(String args[]) {
int z = 100;
int x = 40;
int y = 20;
mul(x,y);
PassByValue pass = new PassByValue(x,y);
mul(pass.a,pass.b);
System.out.println("The Value of Z" + …Run Code Online (Sandbox Code Playgroud) 可能重复:
Java是否通过引用传递?
我在这里有点困惑.Arrays.sort(a)如何修改a的值?
int[] a = {9,8,7,6,5,4,3,2,1};
Arrays.sort(a);
System.out.println(Arrays.toString(a));
Run Code Online (Sandbox Code Playgroud)
我以为java是值得传递的......
我想将一个字符串发送到方法并在那里更改字符串.该方法应返回void.例:
String s = "Hello";
ChangeString(s);
String res = s;
//res = "HelloWorld"
-------------------------------------------
private void ChageString(String s){
s = s + "World";
}
Run Code Online (Sandbox Code Playgroud)
我怎么能用Java做呢?可以在不添加其他类的情况下完成吗?
谢谢!PB
我在Eclipse中遇到了问题.为什么oldListLogCat中的值不同而我在两个Log命令之间没有改变它?
首先我有一个initialize方法:
private void initialize() {
list[0][0] = 2;
list[0][1] = 4;
list[1][0] = 3;
list[1][1] = 7;
oldList = list;
going();
}
Run Code Online (Sandbox Code Playgroud)
在going方法中,我打印了oldList两次:
private void going() {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
Log.i("Log", "oldList = " + oldList[i][j]);
}
}
Log.i("Log", "---------------------------");
// ----------------------------------------------------
list[0][0] = 0;
list[0][1] = 5;
list[1][0] = 0;
list[1][1] = 0; …Run Code Online (Sandbox Code Playgroud) 摘自Oracle的官方Java教程,请参阅此处的问题2(我的样板文件).
public static void main(String[] args) {
String[] students = new String[10];
String studentName = "Peter Smith";
students[0] = studentName;
studentName = null;
System.out.println(students[0]);
}
Run Code Online (Sandbox Code Playgroud)
答案说,studentName由于数组students仍然引用它,因此不符合垃圾收集的条件.然而,最后一行打印"彼得史密斯",所以对我来说,students[0]引用似乎是错误的studentName.有人可以解释一下吗?
java ×10
reference ×2
arrays ×1
break ×1
coding-style ×1
dropwizard ×1
loops ×1
memory ×1
sorting ×1
stack ×1
string ×1
variables ×1
while-loop ×1