简体   繁体   English

如何在Java中查找链表中元素的索引之一是否为零

[英]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). 一个项目可能在此列表的多个索引处(例如,索引0,索引3和索引5)。 How can I find if one of these indexes is index 0; 如何找到这些索引之一是否为索引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. 我需要这样做是因为我使用一些公式来计算某些值,并且如果项目的一个位置为索引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 您可以在列表上使用get方法,在项目上使用equals方法

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

Normally you don't want to use get(int) on a LinkedList because it's an order n traversal. 通常,您不想在LinkedList上使用get(int) ,因为它是n次遍历。 But index 0 will always be at the front and is a very short traversal. 但是索引0将始终位于最前面,并且遍历非常短。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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