我正在阅读Java中的volatile关键字并完全理解它的理论部分.
但是,我正在寻找的是一个很好的案例,它展示了如果变量不是易变的话会发生什么.
下面的代码片段无法正常工作(来自aioobe)
class Test extends Thread {
boolean keepRunning = true;
public void run() {
while (keepRunning) {
}
System.out.println("Thread terminated.");
}
public static void main(String[] args) throws InterruptedException {
Test t = new Test();
t.start();
Thread.sleep(1000);
t.keepRunning = false;
System.out.println("keepRunning set to false.");
}
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,如果keepRunning不是volatile,则线程应该继续无限运行.但是,它会在几秒钟后停止.
我有两个基本问题: -
我对计算二叉树高度的逻辑感到有些困惑.
public static int findHeight(Tree node) {
if(node == null)
return 0;
else {
return 1+Math.max(findHeight(node.left), findHeight(node.right));
}
}
Run Code Online (Sandbox Code Playgroud)
public static int findHeight(Tree node) {
if(node == null)
return -1;
else {
return 1+Math.max(findHeight(node.left), findHeight(node.right));
}
}
Run Code Online (Sandbox Code Playgroud)
我认为,第二个是正确的,因为它给出了以下代码的正确答案: -
Tree t4 = new Tree(4);
Tree t2 = new Tree(2);
Tree t1 = new Tree(1);
Tree t3 = new Tree(3);
Tree t5 = new Tree(5);
t4.left = t2;
t4.right = t5;
t2.left = t1;
t2.right = t3;
// Prints …Run Code Online (Sandbox Code Playgroud) 我正在学习不变性的概念.
我知道一旦创建对象,不可变对象就无法更改它们的值.
但我不明白以下对不可变对象的使用.
他们是
How ? Proof ?How ? Any example ?How ? Any example ?How ? Any example ?"failure atomicity" (a term used by Joshua Bloch):如果一个不可变对象抛出异常,它永远不会处于不受欢迎或不确定的状态.How ? Any example ?有人可以通过支持它的例子详细解释这些要点吗?
谢谢.
我正在尝试删除链表的最后一个节点,只给出了指向该节点的指针。
我写了下面的实现,但是没有用。
我已经访问了有关此主题的大多数SO问题,但是如果只有一个指向该节点的指针,它们都没有显示如何删除链表的最后一个节点?
我在这里想念什么吗?
class Node {
Node next;
int value;
Node(int val) {
this.value = val;
this.next = null;
}
@Override
public String toString() {
Node cur = this;
String str = "";
while(cur != null) {
str += cur.value+"->";
cur = cur.next;
}
return str;
}
}
class DeleteNodeLL {
public static void deleteNode(Node current) {
Node temp;
if(current.next == null) {
current = null;
return;
} else {
current.value = current.next.value;
temp = current.next;
temp = null; …Run Code Online (Sandbox Code Playgroud) 我正在尝试合并两个已排序的链表.
代码段不适用于以下两个列表:
List 1 : 1->3->5->7->9->null
List 2 : 2->4->6->8->10->null
Expected List : 1->2->3->4->5->6->7->8->9->10->null
Run Code Online (Sandbox Code Playgroud)
但是以下程序的输出结果是这样的:
Output : 1->2->3->4->5->6->7->8->9->null // element 10 is missing.
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?现场演示:http://ideone.com/O7MBlo
class Node {
Node next;
int value;
Node(int val) {
this.value = val;
this.next = null;
}
@Override
public String toString() {
Node cur = this;
String str = "";
while(cur != null) {
str += cur.value+"->";
cur = cur.next;
}
return str;
}
}
class MergeLL {
public static Node merge(Node n1, …Run Code Online (Sandbox Code Playgroud) 伙计们,
我最近接受了采访,并在Palindrome上提出了一个问题.
给定一个字符串(可能代表一个日期),使用Stack检查它是否是回文.
我试图提出解决方案,但他并不喜欢这样.
任何人都可以在Java中向我展示它的代码片段吗?
谢谢
PS:这不是作业,实际的面试问题.
java ×6
algorithm ×3
linked-list ×2
string ×2
binary-tree ×1
concurrency ×1
immutability ×1
mergesort ×1
object ×1
palindrome ×1
stack ×1
tree ×1
volatile ×1
wrapper ×1