简体   繁体   中英

How to find if one of the indexes for an element in a linked list is zero in java

I have a linked list. An item may be at several indexes of this list (eg index 0, index 3, and index 5). How can I find if one of these indexes is index 0; I need that because I use some formulas to calculate some values, and the formula is different if one position of the item is index 0.

您可以检查第一个元素是您想要的对象

if (list != null && list.size() > 0 && list.get(0).equals(element))

You can use the get method on list and the equals method on your item

list.get(0).equals(myItem);

Normally you don't want to use get(int) on a LinkedList because it's an order n traversal. But index 0 will always be at the front and is a very short traversal. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM