Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  
 

collections

Ads

 
 

Share on Google+Share on Google+

Manoj Hunasigi
collections
1 Answer(s)      6 years and 4 months ago
Posted in : Java Beginners

please explain me d flow


import java.util.*;

public class Array{
    public static void main(String[] args){
        int arr[]={10,45,25,6,78,12,21};
        int arr1[]={10,45,25,6,78,12,21};
        int arr2[]={10,45,85,45,12,78,296};

        for (int i=0;i<arr.length;i++){
            System.out.println(arr[i]+"\t");}
        for(int i=0;i<arr1.length;i++){
            System.out.println(arr1[i]+"\t");}
        for(int i=0;i<arr2.length;i++){
            System.out.println(arr2[i]+"\t");


        }
        System.out.println("\n ******arr After sorting*****");
        Arrays.sort(arr);
        for (int i=0;i<arr.length;i++){
            System.out.println(arr[i]+"\t");


        }

        System.out.println();
        System.out.println(Arrays.binarySearch(arr,6));
        System.out.println(Arrays.binarySearch(arr,9));
        System.out.println(Arrays.equals(arr,arr1));
        System.out.println(Arrays.equals(arr,arr2));

        Arrays.fill(arr,32);
        for (int i=0;i<arr.length;i++){
            System.out.println(arr[i]+"\t");
        }
        Object ob[]={"jlc","india","sri","vas","white","red"};
        for (int i=0;i<ob.length;i++){
            System.out.println(ob[i]+"\t");
        }
        System.out.println();
        List list= Arrays.asList(ob);
        Iterator it=list.iterator();
        while(it.hasNext()){
            System.out.println(it.next()+"\t");
        }
        System.out.println();
        Arrays.sort(ob);
        for (int i=0;i<ob.length;i++){
            System.out.println(ob[i]+"\t");
        }
        System.out.println();
    }}

Thanks

Ads
View Answers

April 20, 2011 at 11:46 AM


In the given code, 3 arrays of integer type have been defined. Then using the for loop, the array elements have been displayed. Then the method Arrays.sort(arr) sort the values of first array and display the array in the sorted order. The method binarySearch(arr, 6) searches the element 6 in the first array and returns 0 as after sorting the array, the position of the element 6 is changed to 0. As there is no element 9 in the array, so it returns negative value. The method equals compare the two arrays and found that neither arr equals to arr1 nor arr equals to arr2. As the position of element changed in arr after sorting the array. The method Arrays.fill(arr,32) assign the value 32 to each element of the array 'arr'.

Then there is another array of object type has been defined. Then using the Arrats.asList() method, this array has been converted into List which is then iterated using Iterator class and display the values of List. After that, using the method Arrays.sort(), the elements of Object arrat get sorted and displayed.


Ads









Related Tutorials/Questions & Answers:
Collections
Collections  Hi, Please send a study material on collections.../java/jdk6/introduction-collections-api.shtml http://www.roseindia.net/javacodeexamples/index.shtml http://www.devmanuals.com/tutorials/java/collections/index.html
collections
collections  in collections can we perform binarysearch on arraylist elements without sorting them
Advertisements
collections
collections  why we need collections.when we will use collections in our applications
what is collections?
what is collections?  what is collections
Collections API
Collections API  hello, What is the Collections API?   hi The Collections API is a set of classes and interfaces that support operations on collections of objects
Collections in Java
Collections in Java  What is Concurrent Hash map
Collections in Java
Collections in Java  What is Concurrent Hash map
java collections
java collections  what are all the methods and things added for collections in java5 and java6 please kindly reply me as soon as possible
Version of commons-collections>commons-collections dependency
List of Version of commons-collections>commons-collections dependency
Version of excalibur-collections>excalibur-collections dependency
List of Version of excalibur-collections>excalibur-collections dependency
Collections Framework
Collections Framework  Sir, We know that both HashMap & Hashtable is using for same purposes i.e Used for storing keys-values pair. But there is some difference between this two class that are 1)Hashtable is synchronized
collections in java
collections in java  please explain me the flow of this program..i m getting o/p as 2 1 1..y not 2 1 0..is it that if we change the i2 value,it doesnt remove. import java.util.*; public class Mapit { public static void main
Version of commons-collections>commons-collections-testframework dependency
List of Version of commons-collections>commons-collections-testframework dependency