desislava_topuzakova

07. Custom List

Oct 18th, 2020
1,924
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.84 KB | None | 0 0
  1. package CustomList_07;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.List;
  6.  
  7. public class CustomList<E extends Comparable<E>> implements Iterable<E> {
  8.     private List<E> data;
  9.  
  10.     public CustomList() {
  11.         this.data = new ArrayList<>();
  12.     }
  13.  
  14.     public void add(E element) {
  15.         this.data.add(element);
  16.     }
  17.  
  18.     public E remove(int index) {
  19.         if (index < 0 || index > this.data.size()) {
  20.             throw new IndexOutOfBoundsException("Index was out of bounds.");
  21.         }
  22.  
  23.         return this.data.remove(index);
  24.     }
  25.  
  26.     public boolean contains(E element) {
  27.         return this.data.contains(element);
  28.     }
  29.  
  30.     public void swap(int firstIndex, int secondIndex) {
  31.         if (firstIndex < 0 || firstIndex > this.data.size()) {
  32.             throw new IndexOutOfBoundsException("Index was out of bounds.");
  33.         }
  34.  
  35.         if (secondIndex < 0 || secondIndex > this.data.size()) {
  36.             throw new IndexOutOfBoundsException("Index was out of bounds.");
  37.         }
  38.  
  39.         E firstElement = this.data.get(firstIndex);
  40.         E secondElement = this.data.get(secondIndex);
  41.         this.data.set(firstIndex, secondElement);
  42.         this.data.set(secondIndex, firstElement);
  43.     }
  44.  
  45.     public int getCountOfElementsGreaterThan(E element) {
  46.         int count = 0;
  47.         for (int i = 0; i < this.data.size(); i++) {
  48.             if (this.data.get(i).compareTo(element) > 0) {
  49.                 count++;
  50.             }
  51.         }
  52.  
  53.         return count;
  54.     }
  55.  
  56.     public int size() {
  57.         return this.data.size();
  58.     }
  59.  
  60.     public E get(int index) {
  61.         return this.data.get(index);
  62.     }
  63.  
  64.     public E getMax() {
  65.         if (this.data.isEmpty()) {
  66.             throw new IllegalStateException("List is empty.");
  67.         }
  68.  
  69.         E max = this.data.get(0);
  70.         for (int i = 1; i < data.size(); i++) {
  71.             E current = this.data.get(i);
  72.             if (current.compareTo(max) > 0) {
  73.                 max = current;
  74.             }
  75.         }
  76.  
  77.         return max;
  78.     }
  79.  
  80.     public E getMin() {
  81.         if (this.data.isEmpty()) {
  82.             throw new IllegalStateException("List is empty.");
  83.         }
  84.  
  85.         E min = this.data.get(0);
  86.         for (int i = 1; i < data.size(); i++) {
  87.             E current = this.data.get(i);
  88.             if (current.compareTo(min) < 0) {
  89.                 min = current;
  90.             }
  91.         }
  92.  
  93.         return min;
  94.     }
  95.  
  96.     @Override
  97.     public Iterator<E> iterator() {
  98.         return new Iterator<E>() {
  99.             private int index = 0;
  100.  
  101.             @Override
  102.             public boolean hasNext() {
  103.                 return index < data.size();
  104.             }
  105.  
  106.             @Override
  107.             public E next() {
  108.                 return data.get(index++);
  109.             }
  110.         };
  111.     }
  112. }
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. package CustomList_07;
  120.  
  121. import java.io.BufferedReader;
  122. import java.io.IOException;
  123. import java.io.InputStreamReader;
  124.  
  125. public class Main {
  126.     public static void main(String[] args) throws IOException {
  127.  
  128.         BufferedReader reader  = new BufferedReader(new InputStreamReader(System.in));
  129.         CustomList<String> customList = new CustomList<>();
  130.  
  131.         String[] commandTokens = reader.readLine().split("\\s+");
  132.  
  133.         String command = commandTokens[0];
  134.  
  135.         while (!command.equals("END")) {
  136.             switch (command) {
  137.                 case "Add":
  138.                     customList.add(commandTokens[1]);
  139.                     break;
  140.                 case "Remove":
  141.                     customList.remove(Integer.valueOf(commandTokens[1]));
  142.                     break;
  143.                 case "Contains":
  144.                     System.out.println(customList.contains(commandTokens[1]));
  145.                     break;
  146.                 case "Swap":
  147.                     int firstIndex = Integer.valueOf(commandTokens[1]);
  148.                     int secondIndex = Integer.valueOf(commandTokens[2]);
  149.                     customList.swap(firstIndex, secondIndex);
  150.                     break;
  151.                 case "Greater":
  152.                     System.out.println(customList.getCountOfElementsGreaterThan(commandTokens[1]));
  153.                     break;
  154.                 case "Max":
  155.                     System.out.println(customList.getMax());
  156.                     break;
  157.                 case "Min":
  158.                     System.out.println(customList.getMin());
  159.                     break;
  160.                 case "Print":
  161.                     for (String str : customList) {
  162.                         System.out.println(str);
  163.                     }
  164.                     break;
  165.                 default:
  166.                     System.out.println("Invalid command.");
  167.                     break;
  168.             }
  169.  
  170.             commandTokens = reader.readLine().split("\\s+");
  171.             command = commandTokens[0];
  172.         }
  173.     }
  174. }
  175.  
Advertisement
Add Comment
Please, Sign In to add comment