I apologize in advance for the intrusiveness.

But either I don’t understand something, or is there something wrong with the condition of the problem !? Immediately I say that I need to understand it correctly (condition). I will do the implementation myself.

Here is the condition:

Implement the interface mechanism for smart number assignment to list items.

public interface IElementNumberAssigner { /** * Метод выставляет номера {IElement#setupNumber(int)} * для элементов коллекции {elements} * в порядке следования элементов в коллекции. * * Изначально коллекция не содержит элементов, номера которых повторяются. * * При этом обеспечиваются слеюущие условия: * метод работает только с существующими элементами (не создает новых), * на протяжении всей работы метода обеспечивается уникальность номеров элементов: * вызов {element.setNumber(i)} разрешен ⇔ ∀ e ∊ {elements} (e.number ≠ i), * метод устойчив к передаче в него в качестве параметра {java.util.Collections#unmodifiableList(List)} и любой другой реализации immutable-list, * метод должен работать за «приемлемое» время ({IElement#setupNumber(int)} - трудоемкая операция и пользоваться ей надо рационально) * * elements элементы, которым нужно выставить номера */ void assignNumbers(List<IElement> elements); 

}

I have a question: what's the catch?

Is it not possible just to run from the beginning to the end of the list and assign a number to each element in order? What is the difficulty I do not understand?

    2 answers 2

    The difficulty is that the transferred list already has numbers. And in the course of the method's work, the numbers of any two elements should not be allowed to coincide. Those. If a list of 5,4,3,2,1 is transmitted, it is impossible to simply start renumbering with the first element from one, otherwise there will be a doubling of units.

    • "The difficulty is that the list of items being transferred already have numbers." Where is it from? In the original question is not visible. - Igor Kudryashov
    • Read carefully> the {element.setNumber (i)} call is allowed ⇔ ∀ e ∊ {elements} (e.number i) - Yura Ivanov
    • Yes, I didn’t pay attention to these "squiggles" with sets. - Igor Kudryashov
    • Then it is not clear how such a set of elements with numbers 5,4,3,2,1 is generally numbered in the order of the elements in the collection? After all, in any case, I will have to assign its following number to the top element; one ? - Vard32
    • Roughly speaking, in order to set the first element of the collection to the number "1", I need to check whether there is such a number for some other element in the collection? And if it turns out that there is - then temporarily replace "found 1-tsu" with something else?, Then assign the first element to 1? I understand correctly? - Vard32

    the method is resistant to being passed to it as a java.util.Collections#unmodifiableList(List) parameter and any other immutable-list implementation

    the problem is that a collection of objects can be transferred to your method, which cannot be modified, i.e. read-only. You can not write a number in any field of the object. Consequently, the numbers of objects must be stored separately from the objects themselves and, possibly, have some connection between the numbers of objects and the objects themselves, although there is nothing about the connection in your task. But since the assignNumbers method assignNumbers not return anything, it is not clear what to do with these numbers further and how to know in general what they are assigned to objects.

    And there is also a link to some method IElement#setupNumber(int) , you need to somehow use it ...

    • Yes, the fact is that the implementation rules are very clearly stated there: the IElement interface requires you to implement the methods: long getId (); int getNumber (); and void setupNumber (int number); If everything is clear with the first two, then the last one exactly assigns the number int. - Vard32
    • Yes .. and this phrase: "the method works only with existing elements (does not create new ones)" - Vard32
    • That's when everything is clear. You cannot create new items in the passed-in collection method for numbering. - Igor Kudryashov
    • maybe this will help? - Igor Kudryashov
    • Can. But I want to understand the meaning of the problem setting In particular, if we are talking about the order of the elements in the collection, then (as I understand it) I will have to establish this order anyway. I can't sort the elements! - Vard32