The task is to write the class of the variable line in Java. There is a list of elements containing a part of characters of a line (in each on 16). So, in the course of the decision, I had a problem: the changes that occur with the object in the methods are not saved.
class app{ class myObject { char[] symbols; // набор симоволов строки int length; // длина сегмента строки myObject next; // ссылка на след. элемент списка myObject() { /*...*/ } // есть два конструктора, которые создают пустой объект и объект по строке private void split() { // добавляется один элемент myObject newItem = new myObject(); newItem.next = this.next; this.next = newItem; } public void insert(String value, int position) { myObject current = this; // переменная для нахождения нужного элемента, после которого надо вставить новый элемент. /* Цикл нахождения элемента */ current.split(); } } public static void main(String[] args) { myObject val = new myObject("Hello world!"); val.insert("test", 4); val.print(); } }
As a result, when the object is initialized, a list of one element is obtained. Further, after 4 characters, you need to insert "test" and then you should get a list of three elements, but it was not there. He remains the same. Tell me what I'm doing wrong and how to work correctly with objects in methods.
myObject current = this;
- KoVadimmyObject current = this;
hints at terrible code. - KoVadim