1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/* Copy all elements of Java TreeSet to an Object Array Example This Java Example shows how to copy all elements of Java TreeSet object to an array of Objects using toArray method. */ import java.util.TreeSet; public class CopyElementsOfTreeSetToArrayExample { public static void main(String[] args) { //create object of TreeSet TreeSet tSet = new TreeSet(); //add elements to TreeSet object tSet.add(new Integer("1")); tSet.add(new Integer("2")); tSet.add(new Integer("3")); /* To copy all elements of java TreeSet object into array use Object[] toArray() method. */ Object[] objArray = tSet.toArray(); //display contents of Object array System.out.println("TreeSet elements are copied into an Array. Now Array Contains.."); for(int index=0; index < objArray.length ; index++) System.out.println(objArray[index]); } } /* Output would be TreeSet elements are copied into an Array. Now Array Contains.. 1 2 3 */ |
Add Comment