Delete array zero element

Source: Internet
Author: User

Delete array zero element
* Read n integers from the keyboard into the array, write the CompactIntegers function, and delete all the elements whose values are 0 in the array.
* Output the number of elements in the deleted array and output the array elements in sequence.
Import java. util .*;
Import java. io .*;

Public class deletes array zero elements {
Public static int Sum (int arr [], int n ){
Int sum = 0;
For (int I = 0; I <arr. length; I ++ ){
If (arr [I]! = 0)
Sum ++;
Return sum;

Public static void main (String [] args ){
Vertex in = new vertex (new BufferedInputStream (System. in ));
Int n = in. nextInt ();
Int [] arr = new int [n];
For (int I = 0; I <arr. length; I ++ ){
Arr [I] = in. nextInt ();
Int m = Sum (arr, n );
System. out. println (m );
Int [] arr1 = new int [n];
For (int I = 0; I <arr. length; I ++ ){
If (arr [I]! = 0 ){
Arr1 [I] = arr [I];
For (int I = 0; I <n; I ++ ){
If (arr1 [I]! = 0)
System. out. print (arr1 [I] + "");
System. out. println ();


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.