Wednesday, 15 July 2015

Java Programs Made Easy

Q)...Two inputs of a string array and a string is received. The array shld be sorted in descending order. The index of second input in a array shld be retrieved.
input1:{"ga","yb","awe"}
input2:awe
output:2


  1. package Set1;
  2. import java.util.*;
  3. public class ClassSet11 {
  4.  public static void main(String[] args) {
  5.   String[] s1={"good","yMe","awe"};
  6.   String s2="awe";
  7.   System.out.println(stringRetrieval(s1,s2));
  8.  }
  9.  public static int stringRetrieval(String[] s1, String s2){
  10.   ArrayList<String> l1=new ArrayList<String>();
  11.   for(int i=0;i<s1.length;i++)
  12.    l1.add(s1[i]);
  13.   Collections.sort(l1,Collections.reverseOrder()) ;
  14.   System.out.println(l1);
  15.   int n=l1.indexOf(s2);
  16.   return n;
  17.  }
  18. }