Liutauras Vilda wrote:@OP
Don't forget to "think about"/handle the cases when the lists might be not equal in terms of their sizes.
Liutauras Vilda wrote:I thought you'd go with your own implementation (which is fairly simple). Didn't try Guava's this method, but I trust it does the job.
Mike London wrote:The links you gave me pointed to several libraries that did the job.
Mike London wrote:I already had "working code" with two nested list for-loops
Mike London wrote:I find it often difficult to interpolate all the online code examples, into working code.
Liutauras Vilda wrote:But if you are happy with the result you got - all fine
There are three kinds of actuaries: those who can count, and those who can't.
Mike London wrote:I found the code below which appears simpler, but the question I have is why does the "result.add(...)" code call the toString() method? I thought toString() got called when you tried to System.out.println(someobject).
There are three kinds of actuaries: those who can count, and those who can't.
Mike London wrote:I also never realized you could just put any placeholders you wanted (like A and B) in for generic arguments.
Piet Souris wrote:
Mike London wrote:I found the code below which appears simpler, but the question I have is why does the "result.add(...)" code call the toString() method? I thought toString() got called when you tried to System.out.println(someobject).
Well, the 'toString' method is not being called. it1 is an iterator(A) and it2 is an iterator(B) (can't use the angled brackets here, because they have a special meaning in this editor), so the calls to 'next' result in an A and a B being delivered, and from these two a new ListZip object is formed.
It is a bit simpler than mine, but mine is a tad more general, and it also deals somehow with the situation that the lists are unequal of size, This method simply stops at that point.
Mike London wrote:Maybe I misunderstand, but I can set a breakpoint on toString and it runs on the result.add()
Junilu Lacar wrote:
Mike London wrote:Maybe I misunderstand, but I can set a breakpoint on toString and it runs on the result.add()
I think you did misunderstand. The only line that would call toString() is line 21:
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:Indeed, and I don't think that ListZip class is simple. For instance
So, in twoLists we have that A is List and B is List. However, the zip method takes a List<A> and a List<B> as parameters. Very confusing. But take note that the generic A and B from ListZip are not the same as the generic A and B from the zip method!
Edit: my reply was a reply to Junilu's post
Edit2: indeed it is the Object::toString that causes each ListZip to be transformed into a String, and the toString is used for this.
There are three kinds of actuaries: those who can count, and those who can't.
There are three kinds of actuaries: those who can count, and those who can't.
There are three kinds of actuaries: those who can count, and those who can't.
βThe strongest of all warriors are these two β Time and Patience.β β Leo Tolstoy, War and Peace