Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.IntStream;
  3.  
  4. public class BlockContainerTest {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         int n = scanner.nextInt();
  8.         int size = scanner.nextInt();
  9.         BlockContainer<Integer> integerBC = new BlockContainer<Integer>(size);
  10.         scanner.nextLine();
  11.         Integer lastInteger = null;
  12.         for(int i = 0; i < n; ++i) {
  13.             int element = scanner.nextInt();
  14.             lastInteger = element;
  15.             integerBC.add(element);
  16.         }
  17.         System.out.println("+++++ Integer Block Container +++++");
  18.         System.out.println(integerBC);
  19.         System.out.println("+++++ Removing element +++++");
  20.         integerBC.remove(lastInteger);
  21.         System.out.println("+++++ Sorting container +++++");
  22.         integerBC.sort();
  23.         System.out.println(integerBC);
  24.         BlockContainer<String> stringBC = new BlockContainer<String>(size);
  25.         String lastString = null;
  26.         for(int i = 0; i < n; ++i) {
  27.             String element = scanner.next();
  28.             lastString = element;
  29.             stringBC.add(element);
  30.         }
  31.         System.out.println("+++++ String Block Container +++++");
  32.         System.out.println(stringBC);
  33.         System.out.println("+++++ Removing element +++++");
  34.         stringBC.remove(lastString);
  35.         System.out.println("+++++ Sorting container +++++");
  36.         stringBC.sort();
  37.         System.out.println(stringBC);
  38.     }
  39. }
  40.  
  41. // Вашиот код овде
  42.    
  43. class BlockContainer<T extends Comparable>{
  44.    
  45.     private int size;
  46.     private List<TreeSet<T>> block;
  47.    
  48.     BlockContainer(int size){
  49.         this.size = size;
  50.         block = new ArrayList<>();
  51.     }
  52.    
  53.     public void add(T a){
  54.         if(block.size() == 0){
  55.             TreeSet<T> t = new TreeSet<>();
  56.             t.add(a);
  57.             block.add(t);
  58.         }else if(block.get(block.size()-1).size() < size){
  59.             block.get(block.size()-1).add(a);
  60.         }else{
  61.             TreeSet<T> set = new TreeSet<>();
  62.             set.add(a);
  63.             block.add(set);
  64.         }
  65.     }
  66.    
  67.     public boolean remove(T a){
  68.         boolean removed = block.get(block.size()-1).remove(a);
  69.         if(block.get(block.size()-1).size() == 0){
  70.             block.remove(block.size()-1);
  71.         }
  72.         return removed;
  73.     }
  74.    
  75.     public void sort(){
  76.         List<T> elements = new ArrayList<>();
  77.         for(int i = 0 ; i < block.size() ; i++){
  78.             for(T x : block.get(i)){
  79.                 elements.add(x);
  80.             }
  81.         }
  82.         Collections.sort(elements , new Comparator<T>(){
  83.             @Override
  84.             public int compare(T o1, T o2) {
  85.                 return o1.compareTo(o2);
  86.             }
  87.         });
  88.         block.clear();
  89.         for(T x : elements){
  90.             this.add(x);
  91.         }
  92.     }
  93.    
  94.     public String toString(){
  95.         StringBuilder sb = new StringBuilder();
  96.         for(int i = 0 ; i < block.size(); i++){
  97.             sb.append("[");
  98.             for(T x : block.get(i)){
  99.                 if(block.get(i).last().equals(x)){
  100.                     sb.append(x);
  101.                 }else
  102.                 sb.append(x+", ");
  103.             }
  104.             if(i == block.size()-1){
  105.                 sb.append("]");
  106.             }else
  107.             sb.append("],");
  108.         }
  109.         return sb.toString();
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement