Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package CustomList_07;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- public class CustomList<E extends Comparable<E>> implements Iterable<E> {
- private List<E> data;
- public CustomList() {
- this.data = new ArrayList<>();
- }
- public void add(E element) {
- this.data.add(element);
- }
- public E remove(int index) {
- if (index < 0 || index > this.data.size()) {
- throw new IndexOutOfBoundsException("Index was out of bounds.");
- }
- return this.data.remove(index);
- }
- public boolean contains(E element) {
- return this.data.contains(element);
- }
- public void swap(int firstIndex, int secondIndex) {
- if (firstIndex < 0 || firstIndex > this.data.size()) {
- throw new IndexOutOfBoundsException("Index was out of bounds.");
- }
- if (secondIndex < 0 || secondIndex > this.data.size()) {
- throw new IndexOutOfBoundsException("Index was out of bounds.");
- }
- E firstElement = this.data.get(firstIndex);
- E secondElement = this.data.get(secondIndex);
- this.data.set(firstIndex, secondElement);
- this.data.set(secondIndex, firstElement);
- }
- public int getCountOfElementsGreaterThan(E element) {
- int count = 0;
- for (int i = 0; i < this.data.size(); i++) {
- if (this.data.get(i).compareTo(element) > 0) {
- count++;
- }
- }
- return count;
- }
- public int size() {
- return this.data.size();
- }
- public E get(int index) {
- return this.data.get(index);
- }
- public E getMax() {
- if (this.data.isEmpty()) {
- throw new IllegalStateException("List is empty.");
- }
- E max = this.data.get(0);
- for (int i = 1; i < data.size(); i++) {
- E current = this.data.get(i);
- if (current.compareTo(max) > 0) {
- max = current;
- }
- }
- return max;
- }
- public E getMin() {
- if (this.data.isEmpty()) {
- throw new IllegalStateException("List is empty.");
- }
- E min = this.data.get(0);
- for (int i = 1; i < data.size(); i++) {
- E current = this.data.get(i);
- if (current.compareTo(min) < 0) {
- min = current;
- }
- }
- return min;
- }
- @Override
- public Iterator<E> iterator() {
- return new Iterator<E>() {
- private int index = 0;
- @Override
- public boolean hasNext() {
- return index < data.size();
- }
- @Override
- public E next() {
- return data.get(index++);
- }
- };
- }
- }
- package CustomList_07;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class Main {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- CustomList<String> customList = new CustomList<>();
- String[] commandTokens = reader.readLine().split("\\s+");
- String command = commandTokens[0];
- while (!command.equals("END")) {
- switch (command) {
- case "Add":
- customList.add(commandTokens[1]);
- break;
- case "Remove":
- customList.remove(Integer.valueOf(commandTokens[1]));
- break;
- case "Contains":
- System.out.println(customList.contains(commandTokens[1]));
- break;
- case "Swap":
- int firstIndex = Integer.valueOf(commandTokens[1]);
- int secondIndex = Integer.valueOf(commandTokens[2]);
- customList.swap(firstIndex, secondIndex);
- break;
- case "Greater":
- System.out.println(customList.getCountOfElementsGreaterThan(commandTokens[1]));
- break;
- case "Max":
- System.out.println(customList.getMax());
- break;
- case "Min":
- System.out.println(customList.getMin());
- break;
- case "Print":
- for (String str : customList) {
- System.out.println(str);
- }
- break;
- default:
- System.out.println("Invalid command.");
- break;
- }
- commandTokens = reader.readLine().split("\\s+");
- command = commandTokens[0];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment