Thursday, December 13, 2012

String/Array


package charpter1_ArraysAndStrings;

import java.util.Arrays;

public class C1_3 {
public static String Sort(String s)
{
char[] ca = s.toCharArray();
Arrays.sort(ca);
return new String(ca);
}
public static void main(String...args)
{
String s1 = "app";
String s2 = "pap";
System.out.println(Sort(s1).equals(Sort(s2)));
System.out.println(s1);
System.out.println(s2);
char[] ca = s1.toCharArray();
System.out.println(ca.toString());//it is a reference
}
}


1. String -> char[]:  toCharArray() 
2. To sort an Array:  Arrays.sort()
3. char[] -> String:  new String(); Do not use .toString(), it will just return the reference of array. 

No comments:

Post a Comment