Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.IntStream;
- public class BlockContainerTest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = scanner.nextInt();
- int size = scanner.nextInt();
- BlockContainer<Integer> integerBC = new BlockContainer<Integer>(size);
- scanner.nextLine();
- Integer lastInteger = null;
- for(int i = 0; i < n; ++i) {
- int element = scanner.nextInt();
- lastInteger = element;
- integerBC.add(element);
- }
- System.out.println("+++++ Integer Block Container +++++");
- System.out.println(integerBC);
- System.out.println("+++++ Removing element +++++");
- integerBC.remove(lastInteger);
- System.out.println("+++++ Sorting container +++++");
- integerBC.sort();
- System.out.println(integerBC);
- BlockContainer<String> stringBC = new BlockContainer<String>(size);
- String lastString = null;
- for(int i = 0; i < n; ++i) {
- String element = scanner.next();
- lastString = element;
- stringBC.add(element);
- }
- System.out.println("+++++ String Block Container +++++");
- System.out.println(stringBC);
- System.out.println("+++++ Removing element +++++");
- stringBC.remove(lastString);
- System.out.println("+++++ Sorting container +++++");
- stringBC.sort();
- System.out.println(stringBC);
- }
- }
- // Вашиот код овде
- class BlockContainer<T extends Comparable>{
- private int size;
- private List<TreeSet<T>> block;
- BlockContainer(int size){
- this.size = size;
- block = new ArrayList<>();
- }
- public void add(T a){
- if(block.size() == 0){
- TreeSet<T> t = new TreeSet<>();
- t.add(a);
- block.add(t);
- }else if(block.get(block.size()-1).size() < size){
- block.get(block.size()-1).add(a);
- }else{
- TreeSet<T> set = new TreeSet<>();
- set.add(a);
- block.add(set);
- }
- }
- public boolean remove(T a){
- boolean removed = block.get(block.size()-1).remove(a);
- if(block.get(block.size()-1).size() == 0){
- block.remove(block.size()-1);
- }
- return removed;
- }
- public void sort(){
- List<T> elements = new ArrayList<>();
- for(int i = 0 ; i < block.size() ; i++){
- for(T x : block.get(i)){
- elements.add(x);
- }
- }
- Collections.sort(elements , new Comparator<T>(){
- @Override
- public int compare(T o1, T o2) {
- return o1.compareTo(o2);
- }
- });
- block.clear();
- for(T x : elements){
- this.add(x);
- }
- }
- public String toString(){
- StringBuilder sb = new StringBuilder();
- for(int i = 0 ; i < block.size(); i++){
- sb.append("[");
- for(T x : block.get(i)){
- if(block.get(i).last().equals(x)){
- sb.append(x);
- }else
- sb.append(x+", ");
- }
- if(i == block.size()-1){
- sb.append("]");
- }else
- sb.append("],");
- }
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement