1.Java problem
Public classchartest {String str=NewString ("Good"); Char[] ch={' A ', ' B ', ' C '}; Public Static voidMain (String args[]) {intercontents info=Newintercontents (); Chartest ex=Newchartest (); Ex.change (ex.str,ex.ch); System.out.println (Ex.str+ "and"); System.out.println (ex.ch); } Public voidChange (String str,Charch[]) {STR= "Test OK"; ch[0]= ' G ';; }}
The output is:
Goodand
Gbc
Note: Java differs from C++,java only by value (the basic type is usually passed by value, the object is the value of the copy of the object reference, and so is called by value), and CH is changed because it makes a reference copy to the function according to the reference of the CH object. The change of the elements in the array will cause the change of the array object in Ch. In addition, if you give the function no array, only a single char, then the same as STR will not change
2. Implement single-linked list reversal
Public classnode {node next; intvalue; PublicNode (intvalue) { This. Value =value; } PublicNode GetNext () {returnNext; } Public voidSetnext (Node next) { This. Next =Next; } Public intGetValue () {returnvalue; } Public voidSetValue (intvalue) { This. Value =value; } }
/*** * Implementation of single-linked list reversal*/ Public Static voidMain (String args[]) {chartest chartest=Newchartest (); Node Head=NewNode (0); Node Node1=NewNode (1); Node Node2=NewNode (2); Node Node3=NewNode (3); Node Node4=NewNode (4); Node Node5=NewNode (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); } /** * * @paramN-Head node * method: Outputs all value values of the linked list*/ Public voiddisplay (Node N) {if(n!=NULL) {System.out.println ("Value of output:" +N.getvalue ()); Display (N.next); } Else{System.out.println ("Value of output is null"); } } Public Staticnode Reverse1 (node head) { if(Head = =NULL|| Head.getnext () = =NULL) { returnHead} Node Rehead= Reverse1 (Head.getnext ()); Head.getnext (). Setnext (head); Head.setnext (NULL);returnRehead; }
Note: The recursive reversal method is copy.
Android face Test