Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.ArrayList;
- class Set {
- private ArrayList<Boolean> set = new ArrayList<>();
- private int size;
- public Set() { };
- public Set(Set other) {
- this.set = other.set;
- this.size = other.size;
- }
- public int getSize() {
- return size;
- }
- public boolean getElement(int index) {
- return set.get(index);
- }
- private void removeSet() {
- set.removeAll(set);
- }
- public void includeElement(int index) {
- if (index <= size - 1) {
- set.set(index, true);
- } else {
- for (int i = 0; i < index - size; i++) {
- set.add(false);
- }
- set.add(true);
- }
- size = set.size();
- }
- public void excludeElement(int index) {
- if (index <= size - 1) {
- set.set(index, false);
- } else {
- System.out.println("Этого элемента итак нет в множестве");
- }
- }
- public void zeroSet() {
- set.clear();
- set = new ArrayList<>();
- size = 0;
- }
- public void checkElement(int index) {
- if (index <= size - 1) {
- if (set.get(index)) {
- System.out.println("Этот элемент входит в множество");
- } else {
- System.out.println("Этот элемент не входит в множество");
- }
- } else {
- System.out.println("Этот элемент не входит в множество");
- }
- }
- public void equally (Set other) {
- boolean flag = true;
- if (this.size == other.getSize()) {
- for (int i = 0; i < size; i++) {
- if (set.get(i) == other.getElement(i)) {
- flag = true;
- } else {
- flag = false;
- break;
- }
- }
- } else {
- flag = false;
- }
- if (flag) {
- System.out.println("Множества равны");
- } else {
- System.out.println("Множества не равны");
- }
- }
- public void output() {
- System.out.print("Множество: ");
- for (int i = 0; i < size; i++) {
- if (set.get(i)) {
- System.out.print(i + " ");
- }
- }
- System.out.println();
- }
- public void association (Set other) {
- if (this.size > other.size) {
- for (int i = 0; i < this.size - other.size; i++) {
- other.set.add(false);
- }
- } else {
- for (int i = 0; i < other.size - this.size; i++) {
- this.set.add(false);
- }
- size = set.size();
- }
- for (int i = 0; i < size; i++) {
- if (other.getElement(i)) {
- set.set(i, true);
- }
- }
- }
- public void intersection (Set other) {
- if (this.size > other.size) {
- this.size = other.size;
- }
- for (int i = 0; i < size; i++) {
- if (!other.getElement(i)) {
- set.set(i, false);
- }
- }
- }
- public void difference (Set other) {
- if (this.size > other.size) {
- this.size = other.size;
- }
- for (int i = 0; i < size; i++) {
- if (other.getElement(i)) {
- set.set(i, false);
- }
- }
- }
- public String toString() {
- StringBuilder buffer = new StringBuilder();
- for (int i = 0; i < size; i++) {
- if (set.get(i)) {
- buffer.append(i).append("\n");
- }
- }
- return String.valueOf(buffer);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement