写在前面的话:读书破万卷,编码如有神
--------------------------------------------------------------------
下文主要对java.util.LinkedList<E>的4个查询操作进行介绍,主要内容包括:
1、LinkedList常用4个查询操作介绍
参考内容:
1、JDK源码(1.7)
--------------------------------------------------------------------
1、LinkedList常用4个查询操作介绍
(1) int indexOf(Object o)
功能: 查询元素o在此双端队列中的位置(如果不存在则返回-1)
示例代码:
import java.util.LinkedList; public class LinkedListDemo {
public static void main(String[] args) {
/*********测试LinkedList的'int indexOf(Object o)'方法的使用**********/ //创建一个LinkedList对象
LinkedList<Student> linkedList = new LinkedList<Student>(); //创建一个Student对象,并将其添加到LinkedList对象中
Student stu1 = new Student(1,"zhangsan",20);
linkedList.add(stu1); //创建一个Student对象,并将其添加到LinkedList对象中
Student stu2 = new Student(2,"lisi",21);
linkedList.add(stu2); //创建一个Student对象,并将其添加到LinkedList对象中
Student stu3 = new Student(3,"wangwu",22);
linkedList.add(stu3);
System.out.println("linkedList:" + linkedList); System.out.println("linkedList.indexOf(stu2):" + linkedList.indexOf(stu2)); Student stu4 = new Student(4,"erha",22);
System.out.println("linkedList.indexOf(stu4):" + linkedList.indexOf(stu4));
}
} 运行结果:
linkedList:[Student [stuId=1, stuName=zhangsan, stuAge=20], Student [stuId=2, stuName=lisi, stuAge=21], Student [stuId=3, stuName=wangwu, stuAge=22]]
linkedList.indexOf(stu2):1
linkedList.indexOf(stu4):-1
源代码如下:
/*
查询元素o在此双端队列中的位置(如果不存在,则返回-1)
*/
public int indexOf(Object o) {
int index = 0;
if (o == null) {
//当元素o为null时
//循环遍历此双端队列中的每个元素,查找是否有元素为null
for (Node<E> x = first; x != null; x = x.next) {
if (x.item == null)
return index;
index++;
}
} else {
//当元素o不为null时
//循环遍历此双端队列中的每个元素,查找是否有元素的值等于o
for (Node<E> x = first; x != null; x = x.next) {
if (o.equals(x.item))
return index;
index++;
}
}
return -1;
}
(2) int lastIndexOf(Object o)
功能: 查询元素o在此双端队列中最后一次出现的位置(如果不存在则返回-1)
示例代码:
import java.util.LinkedList; public class LinkedListDemo {
public static void main(String[] args) {
/*********测试LinkedList的'int lastIndexOf(Object o)'方法的使用**********/ //创建一个LinkedList对象
LinkedList<Student> linkedList = new LinkedList<Student>(); //创建一个Student对象,并将其添加到LinkedList对象中
Student stu1 = new Student(1,"zhangsan",20);
linkedList.add(stu1); //创建一个Student对象,并将其添加到LinkedList对象中
Student stu2 = new Student(2,"lisi",21);
linkedList.add(stu2); //创建一个Student对象,并将其添加到LinkedList对象中
Student stu3 = new Student(3,"wangwu",22);
linkedList.add(stu3);
linkedList.add(stu2);
linkedList.add(stu3);
System.out.println("linkedList:" + linkedList); System.out.println("linkedList.lastIndexOf(stu2):" + linkedList.lastIndexOf(stu2)); Student stu4 = new Student(4,"erha",22);
System.out.println("linkedList.lastIndexOf(stu4):" + linkedList.lastIndexOf(stu4));
}
} 运行结果:
linkedList:[Student [stuId=1, stuName=zhangsan, stuAge=20], Student [stuId=2, stuName=lisi, stuAge=21], Student [stuId=3, stuName=wangwu, stuAge=22], Student [stuId=2, stuName=lisi, stuAge=21], Student [stuId=3, stuName=wangwu, stuAge=22]]
linkedList.lastIndexOf(stu2):3
linkedList.lastIndexOf(stu4):-1
源代码如下:
/*
查询元素o在此双端队列中最后一次出现的位置(如果不存在,则返回-1)
*/
public int lastIndexOf(Object o) {
int index = size;
if (o == null) {
//当元素o为null时
//循环逆序遍历双端队列中每个节点元素,查询节点元素值为null的节点
for (Node<E> x = last; x != null; x = x.prev) {
index--;
if (x.item == null)
return index;
}
} else {
//当元素o为null时
//循环逆序遍历双端队列中每个节点元素,查询节点元素值等于元素o的节点
for (Node<E> x = last; x != null; x = x.prev) {
index--;
if (o.equals(x.item))
return index;
}
}
return -1;
}
(3) boolean contains(Object o)
功能: 查询此双端队列中是否有元素o
示例代码:
import java.util.LinkedList; public class LinkedListDemo {
public static void main(String[] args) {
/*********测试LinkedList的'boolean contains(Object o)'方法的使用**********/ //创建一个LinkedList对象
LinkedList<Student> linkedList = new LinkedList<Student>(); //创建一个Student对象,并将其添加到LinkedList对象中
Student stu1 = new Student(1,"zhangsan",20);
linkedList.add(stu1); //创建一个Student对象,并将其添加到LinkedList对象中
Student stu2 = new Student(2,"lisi",21);
linkedList.add(stu2); //创建一个Student对象,并将其添加到LinkedList对象中
Student stu3 = new Student(3,"wangwu",22);
linkedList.add(stu3);
linkedList.add(stu2);
linkedList.add(stu3);
System.out.println("linkedList:" + linkedList); System.out.println("linkedList.contains(stu2):" + linkedList.contains(stu2)); Student stu4 = new Student(4,"erha",22);
System.out.println("linkedList.contains(stu4):" + linkedList.contains(stu4));
}
} 运行结果:
linkedList:[Student [stuId=1, stuName=zhangsan, stuAge=20], Student [stuId=2, stuName=lisi, stuAge=21], Student [stuId=3, stuName=wangwu, stuAge=22], Student [stuId=2, stuName=lisi, stuAge=21], Student [stuId=3, stuName=wangwu, stuAge=22]]
linkedList.contains(stu2):true
linkedList.contains(stu4):false
源代码如下:
public boolean contains(Object o) {
//调用indexOf方法返回元素o在此双端队列中的位置
return indexOf(o) != -1;
}
(4) int size()
功能: 返回此双端队列中元素的个数
示例代码:
import java.util.LinkedList; public class LinkedListDemo {
public static void main(String[] args) {
/*********测试LinkedList的'int size()'方法的使用**********/ //创建一个LinkedList对象
LinkedList<Student> linkedList = new LinkedList<Student>(); //创建一个Student对象,并将其添加到LinkedList对象中
Student stu1 = new Student(1,"zhangsan",20);
linkedList.add(stu1); //创建一个Student对象,并将其添加到LinkedList对象中
Student stu2 = new Student(2,"lisi",21);
linkedList.add(stu2); //创建一个Student对象,并将其添加到LinkedList对象中
Student stu3 = new Student(3,"wangwu",22);
linkedList.add(stu3);
linkedList.add(stu2);
linkedList.add(stu3);
System.out.println("linkedList:" + linkedList); System.out.println("linkedList.size():" + linkedList.size());
}
} 运行结果:
linkedList:[Student [stuId=1, stuName=zhangsan, stuAge=20], Student [stuId=2, stuName=lisi, stuAge=21], Student [stuId=3, stuName=wangwu, stuAge=22], Student [stuId=2, stuName=lisi, stuAge=21], Student [stuId=3, stuName=wangwu, stuAge=22]]
linkedList.size():5
源代码如下:
public int size() {
//返回属性size
return size;
}
--------------------------------------------------------------------
java.util.LinkedList<E>系列文章
java.util.LinkedList<E>(1) java.util.LinkedList<E>(2) java.util.LinkedList<E>(3)
java.util.LinkedList<E>(4) java.util.LinkedList<E>(5) java.util.LinkedList<E>(6)
java.util.LinkedList<E>(7) java.util.LinkedList<E>(8)
--------------------------------------------------------------------
相关知识
java.util.Collection<E> java.util.AbstractCollection<E> java.util.List<E>
java.util.AbstractList<E> java.util.Iterator<E> java.util.ListIterator<E>