Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- public class Array<E> {
- private E data[]; // declared to be an Object since it would be too
- // complicated with generics
- private int size;
- public Array(int size) {
- data = (E[]) new Object[size];
- this.size = size;
- }
- public void set(int position, E o) {
- if (position >= 0 && position < size)
- data[position] = o;
- else
- System.out.println("Ne moze da se vmetne element na dadenata pozicija");
- }
- public E get(int position) {
- if (position >= 0 && position < size)
- return data[position];
- else
- System.out.println("Ne e validna dadenata pozicija");
- return null;
- }
- public int getLength() {
- return size;
- }
- public int find(E o) {
- for (int i = 0; i < size; i++){
- if(o.equals(data[i]))
- return i;
- }
- return -1;
- }
- public void insert(int position, E o) {
- // before all we check if position is within range
- if (position >= 0 && position <= size) {
- // first resize the storage array
- E[] newData = (E[]) new Object[size + 1];
- // copy the data prior to the insertion
- for (int i = 0; i < position; i++)
- newData[i] = data[i];
- // insert the new element
- newData[position] = o;
- // move the data after the insertion
- for (int i = position; i < size; i++)
- newData[i + 1] = data[i];
- // replace the storage with the new array
- data = newData;
- size = size + 1;
- }
- }
- public void delete(int position) {
- // before all we check if position is within range
- if (position >= 0 && position < size) {
- // first resize the storage array
- E[] newData = (E[]) new Object[size - 1];
- // copy the data prior to the delition
- for (int i = 0; i < position; i++)
- newData[i] = data[i];
- // move the data after the deletion
- for (int i = position + 1; i < size; i++)
- newData[i - 1] = data[i];
- // replace the storage with the new array
- data = newData;
- size = size - 1;
- }
- }
- public void resize(int newSize) {
- // first resize the storage array
- E[] newData = (E[]) new Object[newSize];
- // copy the data
- int copySize = size;
- if (newSize < size)
- copySize = newSize;
- for (int i = 0; i < copySize; i++)
- newData[i] = data[i];
- // replace the storage with the new array
- data = newData;
- size = newSize;
- }
- //SORTIRANJE
- public static void Sortiranje(Array<Double> niza)
- {
- for(int i=0; i<niza.getLength(); i++)
- {
- for(int j=i+1; j<niza.getLength(); j++)
- {
- if(niza.get(i).compareTo(niza.get(j))==1)
- {
- double temp = niza.get(i);
- niza.set(i, niza.get(j));
- niza.set(j, temp);
- }
- }
- System.out.print(niza.get(i)+" ");
- }
- }
- //NAJMAL ELEMENT
- public static void najmalElement(Array<Double> niza)
- {
- double mini = niza.get(0);
- for(int i=0; i<niza.getLength(); i++)
- {
- if(niza.get(i)<mini)
- {
- mini=niza.get(i);
- }
- }
- System.out.println(mini);
- }
- //ARITMETICKA SREDINA
- public static void aritmetickaSredina(Array<Double> niza)
- {
- double sum=0;
- int n = niza.getLength();
- for(int i=0; i<niza.getLength(); i++)
- {
- sum+=niza.get(i);
- }
- double art;
- art=sum/n;
- System.out.println(art);
- }
- //NAJGOLEM ELEMENT
- public static void najgolemElement(Array<Double> niza)
- {
- double maxi = niza.get(0);
- for(int i=0; i<niza.getLength(); i++)
- {
- if(niza.get(i)>maxi)
- {
- maxi=niza.get(i);
- }
- }
- System.out.println(maxi);
- }
- //BRISHANJE DUPLIKATI
- public static void duplikati(Array<Double> niza)
- {
- for(int i=0; i<niza.getLength(); i++)
- {
- for(int j=i+1; j<niza.getLength(); j++)
- {
- if(niza.get(i).equals(niza.get(j)))
- {
- niza.delete(j);
- }
- }
- System.out.print(niza.get(i)+ " ");
- }
- }
- //ARRAY TO LIST (Mislum oti ne e ok!)
- public static void toList(Array<Double> niza)
- {
- Scanner s = new Scanner(System.in);
- for(int i=0; i<niza.getLength(); i++)
- {
- niza.set(i, s.nextDouble());
- }
- List lList = Arrays.asList(niza);
- System.out.println(lList);
- }
- public static void main(String[] args)
- {
- Scanner s = new Scanner(System.in);
- int n = s.nextInt();
- Array<Double> niza;
- niza = new Array<Double>(n);
- for(int i=0; i<niza.getLength(); i++)
- {
- niza.set(i, s.nextDouble());
- }
- System.out.println("Sortiranje!");
- Sortiranje(niza);
- System.out.println("\nNajmal element!");
- najmalElement(niza);
- System.out.println("Aritmetichka sredina!");
- aritmetickaSredina(niza);
- System.out.println("Najgolem element!");
- najgolemElement(niza);
- System.out.println("Brishenje duplikati!");
- duplikati(niza);
- System.out.println("\nNiza vo lista!");
- toList(niza);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement