Using flow control commands for Array processing

Creating two arrays, one to accommodate the positive, and the other to accommodate the negative elements of the predefined array, and the necessary logic that will execute the drawing of the appropriate elements in the appropriate array. Elements of equal zero should not be inserted into the resulting arrays. It is also necessary to determine the number of duplicates in given array. In this case, the elements that are equal to zero must be counted as well. In doing so, you do not need to print how many times each element of the array repeats, but count and print only those repeating elements and how many times.
Of course, logic has to be applicable for any given array.

Created on: Dec. 13. 2016.

1:  /*  
2:   * Using flow control commands for Array processing  
3:   */  
4:  package jcpa1;  
5:  import java.util.Arrays;  
6:    
7:  /**  
8:   *  
9:   * @author Djordje Gavrilovic  
10:   * Dec 13 2016  
11:   */  
12:  public class Main {  
13:    
14:    public static void main(String[] args) {  
15:        
16:      int [] array = {12,23,-22,0,43,545,-4,-55,43,12,0,-999,-87};  
17:        
18:          // Determining the number of positive or negative elements       
19:      int positive = 0, negative = 0;  
20:      for ( int i = 0; i < array.length; i++ ){  
21:        if( array [i] > 0 )  
22:          positive = positive + 1;  
23:        else if ( array [i] < 0 )  
24:          negative = negative + 1;  
25:      }  
26:      int [] positiveArray = new int [positive];  
27:      int [] negativeArray = new int [negative];  
28:      
29:          // Extracting and adding positive or negative elements  
30:      int p, p1, n, n1;  
31:      for ( p = 0, p1 = 0, n = 0, n1 = 0; p < array.length; p++, n++ ) {  
32:        if ( array [p] > 0 ){  
33:          positiveArray [p1] = array [p];  
34:          p1++;   
35:        }   
36:        else if ( array [n] < 0 ){  
37:          negativeArray [n1] = array [n];  
38:          n1++;      
39:        }    
40:      }  
41:          // Determining and printing the number of duplicates  
42:      int [] array1 = new int [array.length];   
43:      System.arraycopy( array, 0, array1, 0, array.length );  
44:      Arrays.sort ( array1 );  
45:      int d = array1 [0];  
46:      int d1 = -1;   
47:      for ( int j : array1 ) {  
48:        if ( j == d ) {  
49:          d1++;  
50:          continue;  
51:        }   
52:        else if ( d1 > 1 ) {  
53:          System.out.printf ("Element %d shows up %d times.\n", d, d1);  
54:        }  
55:        d1 = 1;  
56:        d = j;  
57:      }  
58:               
59:      System.out.println ( "Array of positive elements: " +   
60:          Arrays.toString ( positiveArray ) );  
61:      System.out.println ( "Array of negative elements: " +   
62:          Arrays.toString ( negativeArray ) );  
63:        
64:    }  
65:      
66:  }  
67:    

Comments