[英]How to check in an array list if an item is immediately followed by another - Java
I am currently stuck on a part of my project.我目前被困在我的项目的一部分上。
I cannot figure out how to (in Java) to check whether in an existing array list, an item is followed immediately by another item.我无法弄清楚如何(在 Java 中)检查现有数组列表中是否有一个项目紧跟另一个项目。 So, for example, if x is followed by y in the array list: a, b, c, x, y, z.
因此,例如,如果在数组列表中 x 后跟 y:a、b、c、x、y、z。
It needs to be a recurring thing as there can be multiple same values in the list, so multiple x and y items for example.它需要是一个重复的事情,因为列表中可以有多个相同的值,例如多个 x 和 y 项目。
Can someone help me with coming up with the code for this?有人可以帮我想出这个代码吗? It needs to be a method that returns true or false.
它需要是一个返回 true 或 false 的方法。
Thank you.谢谢你。
Just loop:只是循环:
for (int i = 0; i < list.size() - 1; i++)
-1... because the LAST element couldn't possibly match, as it is not followed by anything. -1... 因为最后一个元素不可能匹配,因为它后面没有任何东西。
You can retrieve an item at an index as list.get(i)
, and of course the element that follows it with list.get(i + 1)
.您可以以
list.get(i)
检索索引处的项目,当然也可以使用list.get(i + 1)
它list.get(i + 1)
的元素。
Surely you can put it together with this information (I'm loathe to give more; this feels like homework).你当然可以把它和这些信息放在一起(我不愿意提供更多;这感觉就像作业)。
If (list.indexOf(item1) == list.indexOf(item2) -1) { return true ;
如果 (list.indexOf(item1) == list.indexOf(item2) -1) { return true ; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.