标签:head == nbsp isp new code char ram 对象
1.Java问题
public class CharTest { String str=new String("good"); char[] ch={‘a‘,‘b‘,‘c‘}; public static void main(String args[]) { InterContents info=new InterContents(); CharTest ex=new CharTest(); ex.change(ex.str,ex.ch); System.out.println(ex.str+"and"); System.out.println(ex.ch); } public void change(String str,char ch[]) { str="test ok"; ch[0]=‘g‘;; } }
输出结果为:
goodand
gbc
注释:JAVA不同于C++,Java只有按值传递(基本类型就是通常说的按值传递,对象是传对象引用副本的值,所以也叫按值传递),ch之所以改变是因为它根据ch对象的引用制作了一个引用的副本传给函数,而数组里的元素的改变会引起ch这个数组对象的改变。另外你要是给函数不传数组,只传单个char,那么就和str一样不会改变了
2.实现单链表的反转
public class Node { Node next; int value; public Node(int value) { this.value = value; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } }
/** * * 实现单链表的反转 */ public static void main(String args[]) { CharTest charTest=new CharTest(); Node head=new Node(0); Node node1=new Node(1); Node node2=new Node(2); Node node3=new Node(3); Node node4=new Node(4); Node node5=new Node(5); head.setNext(node1); node1.setNext(node2); node2.setNext(node3); node3.setNext(node4); node4.setNext(node5); node5.setNext(null); Node head1=charTest.Reverse1(head); charTest.display(head1); } /** * * @param n 头结点 * 方法:输出链表的所有的value值 */ public void display(Node n){ if(n!=null){ System.out.println("输出的value:"+n.getValue()); display(n.next); } else{ System.out.println("输出的value为NULL"); } } public static Node Reverse1(Node head) { if (head == null || head.getNext() == null) { return head; } Node reHead = Reverse1(head.getNext()); head.getNext().setNext(head); head.setNext(null); return reHead; }
注释:递归反转的方法是copy来的
标签:head == nbsp isp new code char ram 对象
原文地址:http://www.cnblogs.com/galibujianbusana/p/6953169.html