1) Return a new array with only positive numbers from the given array. The elements in the resulting array shall be sorted in the ascending order.
public static int[] retainPositiveNumbers(int[] a) {
List array=new ArrayList();
for(int i : a)
{
if(i>=0)
array.add(i);
}
Object t[];
t=array.toArray();
Arrays.sort(t);
int idx = 0;
int[] arrayInt = new int[array.size()];
for(Integer v : array)
arrayInt[idx++] = v;
return arrayInt;
}
2) Reverse each word in the original String while maintaining the word order.
For example: parameter: "Hello world", result: "olleH dlrow"
public static String reverseWords(String s) {
String arr[]=s.split(" ");
StringBuffer buf=new StringBuffer();
for(int j=0;j
for(int i=arr[j].length()-1;i>=0;i--){
buf.append(arr[j].charAt(i));
}
buf.append(" ");
}
s=buf.toString();
return s;
}
3) Implement Palindrome function.
public static boolean isPalindrome(String s) {
int low = 0;
int high = s.length() - 1;
while(low <>
if(s.charAt(low) != s.charAt(high))
return false;
low++;
high--;
}
return true;
}
4) Return a new array where the order of elements has been reversed from the original array.
public static Object[] reverseArray(Object[] a) {
Object t[];
t=new Object[a.length];
for(int i=a.length-1,j=0;i>=0;i--,j++)
t[j]=a[i];
return t;
}

2 comments:
comment!!!
super ya!!!!!!!!!!!
Post a Comment