博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android面试题
阅读量:6878 次
发布时间:2019-06-26

本文共 2258 字,大约阅读时间需要 7 分钟。

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来的

转载于:https://www.cnblogs.com/galibujianbusana/p/6953169.html

你可能感兴趣的文章
Linux - 一次运行多个命令
查看>>
10.C# -- 函数参数,参数数组,值传递函数,引用传递函数,输出函数,无参函数...
查看>>
BT5设置ip地址
查看>>
转载/验证码
查看>>
Surface、SurfaceView、SurfaceHolder和SurfaceHolder.Callback之间的联系
查看>>
什么是Data Store and Data Collector?
查看>>
我的友情链接
查看>>
php培训11.30
查看>>
Effective Java读后感
查看>>
windows下两个无线网卡 一个内网 一个外网
查看>>
tcp nat 负载均衡
查看>>
起点,游戏开发的梦想与技能点
查看>>
MPLS 流量工程的配置与各大属性调整详解
查看>>
107个常用Javascript语句
查看>>
我的友情链接
查看>>
Dataram_RAMDisk_v4_0_0安装和配置
查看>>
在window XP下使用vsphere client 5.5 访问vCenter 或者 ESXi5.5 连接错误
查看>>
35 个超棒的 Coming Soon 页面设计案例
查看>>
C语言第四天(位运算)
查看>>
硬RAID可以为NVMe SSD数据可靠性保驾护航吗?
查看>>