[英]Kotlin check if two lists are the same apart from one list having an additional element?
So I have list A and list B.所以我有清单A和清单B。
A is of size n and B is of size n + 1. A 的大小为 n,B 的大小为 n + 1。
I need to know if all elements in A are the same as all elements in B.sublist(0, n - 1).我需要知道 A 中的所有元素是否与 B.sublist(0, n - 1) 中的所有元素相同。
A: [5,7,2,9]
B: [5,7,2,9,1]
true
A: [5,7,2,9]
B: [5,7,2,9,9]
true
A: [7,5,2,9]
B: [5,7,2,9,1]
false (order matters)
Is there any elegant ways to do this using library functions maybe?是否有任何优雅的方法可以使用库函数来做到这一点?
Very simple solution:非常简单的解决方案:
return B.take(A.size+1) == A
As long as b
cannot be empty, you could write:只要
b
不能为空,你就可以写:
return a == b.dropLast(1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.