Write a program to read an integer array and remove all 10s from the
array, shift the other elements towards left and fill the trailing empty
positions by 0 so that the modified array is of the same length of the given
array.
Include a class UserMainCode with a static method removeTens which
accepts the number of elements and an integer array. The return type (Integer
array) should return the final array.
Create a Class Main which would be used to read the number of elements
and the input array, and call the static method present in UserMainCode.
Input and Output Format:
Input consists of n+1 integers, where n corresponds to size of the array
followed by n elements of the array.
Output consists of an integer array (the final array).
Refer sample output for formatting specifications.
Sample Input :
5
1
10
20
10
2
Sample Output :
1
20
2
o
o
import java.util.Scanner;
public class Pro1
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
Integer[] s=UserMainCode.getDigitSum(n,a);
for(int i=0;i<s.length;i++)
{
System.out.println(s[i]);
}
}
}
import java.util.ArrayList;
import java.util.Iterator;
public class UserMainCode {
public static Integer[] getDigitSum(int n,int[] a)
{
ArrayList<Integer> al=new ArrayList<Integer>();
ArrayList<Integer> op=new ArrayList<Integer>();
for(int i=0;i<n;i++)
{
al.add(a[i]);
}
Iterator<Integer> itr=al.iterator();
while(itr.hasNext())
{
int x=itr.next();
if(x!=10)
{
op.add(x);
}
}
if(op.size()<n)
{
n=n-op.size();
for(int i=0;i<n;i++)
{
op.add(0);
}
}
Integer m[]=new Integer[op.size()];
op.toArray(m);
return m;
}
}
0 comments :
Post a Comment