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; } 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.

  • insert method does not use input parameters. Moreover, it makes the strange - myObject current = this; - KoVadim
  • I have, for short, removed all unnecessary code. the point is that the insertion of a new element in this way is not preserved - Vitaly Leonov
  • You removed too much code. And what remains is smacks of delirium. Give all the code. If it is very large / contains confidential data - make a minimal example that reproduces your problem. Once again I will clarify - a line of the form myObject current = this; hints at terrible code. - KoVadim
  • Offhand, split () should work (in the sense of inserting an empty element into the list, after this one). But everything else is unclear (no code). - avp
  • all code, only the program is not ready yet, so something may not seem logical - Vitaly Leonov

1 answer 1

@ Vitaliy Leonov is a prerequisite for being implemented in a list? Something that confuses me the lines of this type, for example (given from the method insertPart2):

 while(after != current) { current =; } 

Why simply not to take? Do not call better the methods method1, method2, method3, etc., then you will not understand.

Why you should have 3 elements, you have 16 characters in each element of "Hello world!" - 12 characters, "test" - 4 characters, when inserting, 16 characters should come out, that is, one element. But you insert it again, as it is strange. In your example in the insertPart2 method (at least if I understood correctly) the following happens.

afterCurrent becomes equal to null (because the line you have is just one element and it will be current and = null), then you insert it into (the next element) the line inserted after inserted an empty element (taken by the value afterCurrent ). Accordingly, you have 2 items not completely filled.

Your length () method returns 16 (because you have 12 for the first element, 4 for the second element)

Well, it displays all 16 characters from the first line (including 4 empty characters), and it does not reach the 2nd element

PS: write a method that displays each element and its length (for verification)

  • @rasmisha, not exactly as you describe (of course, you need to look at "pastebin", and not the text in question). Notice the main String value = "Hello world! Hello world!"; Get the initial state - a list of 2 elements, in the first 16 bytes. Insert is ordered at position 8, i.e. in the middle of the first list item. The nonsense is programmed there now, so the "test" is inserted by a new element of the list (the 2nd) with a length of 4 (ie, at position 16). - Once again I urge the author of the question to describe the ideas of the algorithm that he wants to do and discuss them first. - avp
  • Well, I looked at the code in pastebin, except for main (however, the insertion is still not true) - rasmisha
  • And I do the same thing (which is wrong). Either there are a lot of errors in the program, or the algorithm is not finalized, or ... - avp