Advertisement
Guest User

Untitled

a guest
Oct 9th, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.60 KB | None | 0 0
  1. package realstringapplication;
  2.  
  3. public class RealStringApplication {
  4.  
  5.     public static class realString {
  6.         char[] symbol; // массив символов строки
  7.         int length; // количество заполненных ячеек
  8.         realString next; // следующий элемент списка
  9.        
  10.         realString() {
  11.             symbol = new char[16];
  12.             length = 0;
  13.             next = null;
  14.         }
  15.        
  16.         realString(String value) {
  17.             symbol = new char[16];
  18.             length = 0;
  19.             next = null;
  20.             realString currentItem = this;
  21.             for(int key=0; key < value.length(); key++) {
  22.                 if(key != 0 && key%16 == 0) { // переход к следующему элементу списка
  23.                     currentItem.next = new realString();
  24.                     currentItem = currentItem.next;
  25.                 }
  26.                 currentItem.symbol[currentItem.length] = value.charAt(key);
  27.                 currentItem.length++;
  28.             }
  29.         }
  30.        
  31.         private void separate(int position) {
  32.             /*
  33.              * Разделение элемента списка на два
  34.              **/
  35.              
  36.             System.out.println("separate");
  37.            
  38.             char[] newSymbolsPart = new char[this.length - position];
  39.             int arrLen = this.length;
  40.             int index = 0;
  41.            
  42.             for(int key=position; key<arrLen; key++) {
  43.                 newSymbolsPart[index] = this.symbol[key];
  44.                 //this.symbol.removeAt(key);
  45.                 this.length--;
  46.                 index++;
  47.             }
  48.            
  49.             realString newItem = new realString(new String(newSymbolsPart));
  50.             //newItem.realPrint();
  51.            
  52.             newItem.next = this.next;
  53.             this.next = newItem;
  54.         }
  55.        
  56.         private void insertPart(char[] str, int position) {
  57.             /*
  58.              * Вставка символьного массива в структуру списка на определенную позицию
  59.              **/
  60.             System.out.println("insertPart");
  61.            
  62.             int posInItem = position%16;
  63.            
  64.             realString currentItem = this;
  65.             int counter = 0;
  66.            
  67.             while(currentItem != null) {
  68.                 counter += currentItem.length;
  69.                 if(counter > position) break;
  70.                 currentItem = currentItem.next;
  71.             }
  72.            
  73.             currentItem.separate(posInItem);
  74.             realString inserted = new realString();
  75.         }
  76.        
  77.         private void insertPart2(realString inserted, realString after) {
  78.             realString current = this;
  79.             realString afterCurrent;
  80.            
  81.             while(after != current) {
  82.                 current = current.next;
  83.             }
  84.            
  85.             afterCurrent = current.next;
  86.             current.next = inserted;
  87.            
  88.             realString endOfInserted = inserted;
  89.            
  90.             while(endOfInserted.next != null) {
  91.                 endOfInserted = inserted.next;
  92.             }
  93.            
  94.             endOfInserted.next = afterCurrent;
  95.         }
  96.        
  97.         public void realPrint() {
  98.             int stringLength = this.length(); // реальная длина строки
  99.             realString currentItem = this;
  100.             int index = 0; // индекс символа в массиве в каждом из элементов списка
  101.             for(int i=0; i<stringLength; i++) {
  102.                 if(i != 0 && i%16 == 0) { // переход к следующему элементу списка и обнуление индекса символа
  103.                     currentItem = currentItem.next;
  104.                     index = 0;
  105.                     System.out.println("");
  106.                 }
  107.                 System.out.print(currentItem.symbol[index]);
  108.                 index++;
  109.             }
  110.             System.out.println("");
  111.         }
  112.    
  113.         public void insert(String value, int position) {
  114.         /*
  115.             Вставка строки (value) в изменяемую строку
  116.             на позицию (position)
  117.         */
  118.             System.out.println("insert");
  119.            
  120.             realString currentItem = this;
  121.             int counter = 0;
  122.             char[] string = new char[value.length()];
  123.             int posInItem = position%16; // номер символа в элементе списка после которого надо вставить строку
  124.            
  125.             while(currentItem != null) {
  126.                 counter += currentItem.length;
  127.                 if(counter > position) break;
  128.                 currentItem = currentItem.next;
  129.             }
  130.            
  131.             /*for(int key=0; key<value.length(); key++){ // преобразование строки в массив символов
  132.                 string[key] = value.charAt(key);
  133.             }*/
  134.            
  135.             //this.insertPart(string, position);
  136.            
  137.             //currentItem.separate(posInItem);
  138.             realString inserted = new realString(value);
  139.             this.insertPart2(inserted, currentItem);
  140.         }
  141.        
  142.         public int length() {
  143.         /*
  144.             Получение реальной длины изменяемой строки
  145.         */
  146.             realString item = this;
  147.             int counter = 0;
  148.             while(item != null) {
  149.                 counter += item.length;
  150.                 item = item.next;
  151.             }
  152.             return counter;
  153.         }
  154.        
  155.     }
  156.    
  157.     public static void main(String[] args) {
  158.         String value = "Hello world! Hello world!";
  159.         realString str = new realString(value);
  160.         str.realPrint();
  161.         str.insert("test", 8);
  162.         System.out.print("str: ");
  163.         str.realPrint();
  164.         System.out.println("length: " + value.length() + "; list: " + str.length());
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement