I don't really see a violation here.
The Law of Demeter, in concept, proposes that an entity only knows of its neighbors, not strangers, so if A
needs something from C
, it should talk to B
to get it and not go any further.
I wouldn't exactly call a List
a "neighbor" to apply to this concept. It's an object that manipulates a data structure which you can use throughout your program. Therefore, the Object
that your list contains would be considered the B
to your A
.
If List
was an actual entity defined in your program, you could be right in this case. It would make more sense to have your List
have a method which calls for your Object
to perform some logic (as you said in a comment, list.callSomethingOn(i)
).
If this particular instance of List
is central to your program's logic and you're adamant about satisfying the law, you could use a "Decorator" for the List
which adds additional methods to work with the contained Object
s