Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package realstringapplication;
- public class RealStringApplication {
- public static class realString {
- char[] symbol; // массив символов строки
- int length; // количество заполненных ячеек
- realString next; // следующий элемент списка
- realString() {
- symbol = new char[16];
- length = 0;
- next = null;
- }
- realString(String value) {
- symbol = new char[16];
- length = 0;
- next = null;
- realString currentItem = this;
- for(int key=0; key < value.length(); key++) {
- if(key != 0 && key%16 == 0) { // переход к следующему элементу списка
- currentItem.next = new realString();
- currentItem = currentItem.next;
- }
- currentItem.symbol[currentItem.length] = value.charAt(key);
- currentItem.length++;
- }
- }
- private void separate(int position) {
- /*
- * Разделение элемента списка на два
- **/
- System.out.println("separate");
- char[] newSymbolsPart = new char[this.length - position];
- int arrLen = this.length;
- int index = 0;
- for(int key=position; key<arrLen; key++) {
- newSymbolsPart[index] = this.symbol[key];
- //this.symbol.removeAt(key);
- this.length--;
- index++;
- }
- realString newItem = new realString(new String(newSymbolsPart));
- //newItem.realPrint();
- newItem.next = this.next;
- this.next = newItem;
- }
- private void insertPart(char[] str, int position) {
- /*
- * Вставка символьного массива в структуру списка на определенную позицию
- **/
- System.out.println("insertPart");
- int posInItem = position%16;
- realString currentItem = this;
- int counter = 0;
- while(currentItem != null) {
- counter += currentItem.length;
- if(counter > position) break;
- currentItem = currentItem.next;
- }
- currentItem.separate(posInItem);
- realString inserted = new realString();
- }
- private void insertPart2(realString inserted, realString after) {
- realString current = this;
- realString afterCurrent;
- while(after != current) {
- current = current.next;
- }
- afterCurrent = current.next;
- current.next = inserted;
- realString endOfInserted = inserted;
- while(endOfInserted.next != null) {
- endOfInserted = inserted.next;
- }
- endOfInserted.next = afterCurrent;
- }
- public void realPrint() {
- int stringLength = this.length(); // реальная длина строки
- realString currentItem = this;
- int index = 0; // индекс символа в массиве в каждом из элементов списка
- for(int i=0; i<stringLength; i++) {
- if(i != 0 && i%16 == 0) { // переход к следующему элементу списка и обнуление индекса символа
- currentItem = currentItem.next;
- index = 0;
- System.out.println("");
- }
- System.out.print(currentItem.symbol[index]);
- index++;
- }
- System.out.println("");
- }
- public void insert(String value, int position) {
- /*
- Вставка строки (value) в изменяемую строку
- на позицию (position)
- */
- System.out.println("insert");
- realString currentItem = this;
- int counter = 0;
- char[] string = new char[value.length()];
- int posInItem = position%16; // номер символа в элементе списка после которого надо вставить строку
- while(currentItem != null) {
- counter += currentItem.length;
- if(counter > position) break;
- currentItem = currentItem.next;
- }
- /*for(int key=0; key<value.length(); key++){ // преобразование строки в массив символов
- string[key] = value.charAt(key);
- }*/
- //this.insertPart(string, position);
- //currentItem.separate(posInItem);
- realString inserted = new realString(value);
- this.insertPart2(inserted, currentItem);
- }
- public int length() {
- /*
- Получение реальной длины изменяемой строки
- */
- realString item = this;
- int counter = 0;
- while(item != null) {
- counter += item.length;
- item = item.next;
- }
- return counter;
- }
- }
- public static void main(String[] args) {
- String value = "Hello world! Hello world!";
- realString str = new realString(value);
- str.realPrint();
- str.insert("test", 8);
- System.out.print("str: ");
- str.realPrint();
- System.out.println("length: " + value.length() + "; list: " + str.length());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement