Previous | Home | Next |
In java if you want to sort an array, you can use sort() method defined in the java.util.Arrays class. You can use the method both on the primitive type as well as on the object type arrays.
Example
int [] arr = new int[100]; /* some logic */ Arrays.sort(arr);
Following program shows how to use sort() method for sorting an array :
import java.util.Arrays; Java Tutorialslass sort { public static void main(String args[]) { String[] arr=new String[]{"shashi", "employee", "r4r", "software", "engineer"}; Arrays.sort(arr); for(int i=0; i<arr.length;i++) System.out.println(arr[i]); } }
In java if you want to copy all values of one array to another, you have to use the arraycopy method defined in the java.lang.System class. The general form to call this method is :
System.arraycopy(copyfromarray,startingindex,copytoarray,fromindex,numberofelements);
Following example gives you the brief knowledge about how to use arraycopy() method:
Java Tutorialsclass Java Tutorialscopyarr { public static void main(String args[]) { String[] arr=new String[]{"shashi", "employee", "r4r", "software", "engineer"}; String[] arr1=new String[3]; System.arraycopy(arr,0,arr1,0,3); System.out.println("The contents of new array :"); for(int i=0;i<arr1.length;i++) System.out.println(arr1[i]); } }
Previous | Home | Next |