Tag: reference

如何在Java中检查链表是否是回文?

我写了一个代码来检查单链表是否是回文。 我做了两个步骤: 1。 反转原始链表。 第2位。 检查原始链表和反向链表是否具有相同的元素。 public static Boolean isPalindrome(Node input){ Node reversed= reverse(input); while (input!=null){ if(input.item!=reversed.item) return false; input=input.next; reversed=reversed.next; } return true; } static Node head; public static Node reverse(Node input){ if(input==null || input.next==null){ head=input; return input; } else{ reverse(input.next); input.next.next=input; input.next=null; return head; } } 这个程序有效。 但是我想,当执行反向方法时,由于原始链表的头被传入,所以原始链表也可能会改变,所以isPalindrome也应该返回true,对吧? 我是对的还是你可以告诉我,如果我误解了任何概念? 谢谢 这是主要function以及我如何使用该代码: public static […]

Enum的枚举为NULL

我正在为Java 1.6上的大学课程开发一个LALG编译器。 所以我做了一个类类和语法类。 EnumTypes public enum EnumTypes { A(“OLA”), B(“MUNDO”), C(“HELLO”), D(“WORLD”), /** * The order below is reversed on purpose. * Revert it and will you get a NULL list of types furder. */ I(EnumGrammar.THREE), H(EnumGrammar.TWO), F(EnumGrammar.ONE), E(EnumGrammar.ZERO); private String strValue; private EnumGrammar enumGrammarValue; private EnumTypes(String strValue) { this.strValue = strValue; } private EnumTypes(EnumGrammar […]

未初始化的int与整数

我正在研究我的Java以准备考试,我遇到了一些未初始化的int / Integer值的问题。 class A { int x; Integer y; static int z; static Integer z2; public A(){} } 假设我初始化了A类的对象.A a = new A(); 我在编译器中尝试了这个并获得了结果 ax == 0; true ax == null; Static Error: Bad type in comparison expression ay == 0; java.lang.NullPointerException ay == null; true az == 0; true az == null; Static […]

多个重载方法:null是否等于NullPointerException?

public class TestMain { public static void methodTest(Exception e) { System.out.println(“Exception method called”); } public static void methodTest(Object e) { System.out.println(“Object method called”); } public static void methodTest(NullPointerException e) { System.out.println(“NullPointerException method called”); } public static void main(String args[]) { methodTest(null); } } 输出:调用NullPointerException方法

仅将ArrayList作为值传递而不是引用

简单地说,我有一个带有ArrayList参数的方法。 在方法中,我修改ArrayList的内容,仅用于与方法返回的内容相关的目的。 因此,我不希望作为参数传递的ArrayList完全受到影响(即不作为引用传递)。 我尝试的一切都未能达到预期的效果。 我需要做什么才能在方法中使用ArrayList的副本,但不能更改实际变量?

何时在Java中使用幻像引用?

可能重复: 你有没有在任何项目中使用Phantom参考? 我已经阅读了不同类型的参考资料。 我理解强大,软弱的参考是如何运作的。 但是,当我读到幻影参考时,我无法理解它们。 也许是因为我找不到任何好的例子来告诉我他们的目的是什么或何时使用它们。 你能告诉我一些使用幻像参考的代码示例吗?

java引用之间的xor操作

我想为xor-linked列表编写java代码。 有人可以建议我如何在引用之间执行xor操作吗?