
public
class positive {
public static void main(String[] args) {
retainPositiveNumbers(a);
}
private
static int n;
private
static int j;
static
int a[]={-1,2,3,4,-5};
static
int c[]={};
public static int[] retainPositiveNumbers(int[] a) {
/*
Please implement this method to
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.
*/
?
for(int i=0; i<a.length; i++)
{
j=0;
if(a[i]>0)
{
c[j]=a[i];
j++;
}
}
for(int k=0;k<c.length;k++){
System.out.println(c);
}
return c ;
}
}

This example finds the positive integers from the given array and store these positive integers in another array.
import java.util.*;
public class positive {
public static void main(String[] args) {
int a[]={-1,2,3,4,-5};
retainPositiveNumbers(a);
}
public static void retainPositiveNumbers(int[] a) {
List<Integer> list=new ArrayList<Integer>();
for(int i=0; i<a.length;i++){
if(a[i]>0){
list.add(new Integer(a[i]));
}
}
int[] c = new int[list.size()];
for (int i=0; i < c.length; i++){
c[i] = list.get(i).intValue();
}
for (int i=0; i < c.length; i++){
System.out.println(c[i]);
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.