Advertisement
AnaGocevska

Низа :)

Oct 19th, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.67 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class Array<E> {
  6.     private E data[]; // declared to be an Object since it would be too
  7.                         // complicated with generics
  8.     private int size;
  9.  
  10.     public Array(int size) {
  11.         data = (E[]) new Object[size];
  12.         this.size = size;
  13.     }
  14.  
  15.     public void set(int position, E o) {
  16.         if (position >= 0 && position < size)
  17.             data[position] = o;
  18.         else
  19.             System.out.println("Ne moze da se vmetne element na dadenata pozicija");
  20.     }
  21.  
  22.     public E get(int position) {
  23.         if (position >= 0 && position < size)
  24.             return data[position];
  25.         else
  26.             System.out.println("Ne e validna dadenata pozicija");
  27.         return null;
  28.     }
  29.  
  30.     public int getLength() {
  31.         return size;
  32.     }
  33.    
  34.     public int find(E o) {
  35.         for (int i = 0; i < size; i++){
  36.             if(o.equals(data[i]))
  37.                 return i;
  38.         }
  39.         return -1;
  40.     }
  41.  
  42.     public void insert(int position, E o) {
  43.         // before all we check if position is within range
  44.         if (position >= 0 && position <= size) {
  45.             // first resize the storage array
  46.             E[] newData = (E[]) new Object[size + 1];
  47.             // copy the data prior to the insertion
  48.             for (int i = 0; i < position; i++)
  49.                 newData[i] = data[i];
  50.             // insert the new element
  51.             newData[position] = o;
  52.             // move the data after the insertion
  53.             for (int i = position; i < size; i++)
  54.                 newData[i + 1] = data[i];
  55.             // replace the storage with the new array
  56.             data = newData;
  57.             size = size + 1;
  58.         }
  59.     }
  60.  
  61.     public void delete(int position) {
  62.         // before all we check if position is within range
  63.         if (position >= 0 && position < size) {
  64.             // first resize the storage array
  65.             E[] newData = (E[]) new Object[size - 1];
  66.             // copy the data prior to the delition
  67.             for (int i = 0; i < position; i++)
  68.                 newData[i] = data[i];
  69.             // move the data after the deletion
  70.             for (int i = position + 1; i < size; i++)
  71.                 newData[i - 1] = data[i];
  72.             // replace the storage with the new array
  73.             data = newData;
  74.             size = size - 1;
  75.         }
  76.     }
  77.  
  78.     public void resize(int newSize) {
  79.         // first resize the storage array
  80.         E[] newData = (E[]) new Object[newSize];
  81.         // copy the data
  82.         int copySize = size;
  83.         if (newSize < size)
  84.             copySize = newSize;
  85.         for (int i = 0; i < copySize; i++)
  86.             newData[i] = data[i];
  87.         // replace the storage with the new array
  88.         data = newData;
  89.         size = newSize;
  90.     }
  91.  
  92.  
  93. //SORTIRANJE
  94. public static void Sortiranje(Array<Double> niza)
  95. {
  96.     for(int i=0; i<niza.getLength(); i++)
  97.     {
  98.         for(int j=i+1; j<niza.getLength(); j++)
  99.         {
  100.             if(niza.get(i).compareTo(niza.get(j))==1)
  101.             {
  102.                 double temp = niza.get(i);
  103.                 niza.set(i, niza.get(j));
  104.                 niza.set(j, temp);
  105.                
  106.             }
  107.         }
  108.         System.out.print(niza.get(i)+" ");
  109.     }
  110. }
  111.  
  112. //NAJMAL ELEMENT
  113. public static void najmalElement(Array<Double> niza)
  114. {
  115.     double mini = niza.get(0);
  116.     for(int i=0; i<niza.getLength(); i++)
  117.     {
  118.         if(niza.get(i)<mini)
  119.         {
  120.             mini=niza.get(i);
  121.         }
  122.     }
  123.  
  124.     System.out.println(mini);
  125. }
  126. //ARITMETICKA SREDINA
  127. public static void aritmetickaSredina(Array<Double> niza)
  128. {
  129.     double sum=0;
  130.     int n = niza.getLength();
  131.     for(int i=0; i<niza.getLength(); i++)
  132.     {
  133.         sum+=niza.get(i);
  134.     }
  135.    
  136.     double art;
  137.    
  138.     art=sum/n;
  139.    
  140.     System.out.println(art);
  141. }
  142. //NAJGOLEM ELEMENT
  143. public static void najgolemElement(Array<Double> niza)
  144. {
  145.     double maxi = niza.get(0);
  146.     for(int i=0; i<niza.getLength(); i++)
  147.     {
  148.         if(niza.get(i)>maxi)
  149.         {
  150.             maxi=niza.get(i);
  151.         }
  152.     }
  153.    
  154.     System.out.println(maxi);
  155. }
  156. //BRISHANJE DUPLIKATI
  157. public static void duplikati(Array<Double> niza)
  158. {
  159.     for(int i=0; i<niza.getLength(); i++)
  160.     {
  161.         for(int j=i+1; j<niza.getLength(); j++)
  162.         {
  163.             if(niza.get(i).equals(niza.get(j)))
  164.             {
  165.                 niza.delete(j);
  166.             }
  167.         }
  168.         System.out.print(niza.get(i)+ " ");
  169.     }
  170. }
  171. //ARRAY TO LIST (Mislum oti ne e ok!)
  172. public static void toList(Array<Double> niza)
  173. {
  174.  
  175.     Scanner s = new Scanner(System.in);
  176.    
  177.     for(int i=0; i<niza.getLength(); i++)
  178.     {
  179.         niza.set(i, s.nextDouble());
  180.     }
  181.            
  182.     List lList = Arrays.asList(niza);
  183.    
  184.    
  185.     System.out.println(lList);
  186. }
  187.  
  188.  
  189.  
  190. public static void main(String[] args)
  191. {
  192.         Scanner s = new Scanner(System.in);
  193.        
  194.         int n = s.nextInt();
  195.        
  196.         Array<Double> niza;
  197.        
  198.         niza = new Array<Double>(n);
  199.        
  200.         for(int i=0; i<niza.getLength(); i++)
  201.         {
  202.             niza.set(i, s.nextDouble());
  203.         }
  204.    
  205.         System.out.println("Sortiranje!");
  206.         Sortiranje(niza);
  207.         System.out.println("\nNajmal element!");
  208.         najmalElement(niza);
  209.         System.out.println("Aritmetichka sredina!");
  210.         aritmetickaSredina(niza);
  211.         System.out.println("Najgolem element!");
  212.         najgolemElement(niza);
  213.         System.out.println("Brishenje duplikati!");
  214.         duplikati(niza);
  215.         System.out.println("\nNiza vo lista!");
  216.         toList(niza);
  217.        
  218.  }
  219.  
  220.  
  221.  
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement