写在前面的话:读书破万卷,编码如有神
--------------------------------------------------------------------
下文主要对java.util.LinkedList<E>的4个查询操作进行介绍,主要内容包括:
1、LinkedList常用4个查询操作介绍
参考内容:
1、JDK源码(1.7)
--------------------------------------------------------------------
1、LinkedList常用4个查询操作介绍
(1) int indexOf(Object o)
功能: 查询元素o在此双端队列中的位置(如果不存在则返回-1)
示例代码:
1 import java.util.LinkedList; 2 3 public class LinkedListDemo { 4 public static void main(String[] args) { 5 /*********测试LinkedList的'int indexOf(Object o)'方法的使用**********/ 6 7 //创建一个LinkedList对象 8 LinkedList<Student> linkedList = new LinkedList<Student>(); 9 10 //创建一个Student对象,并将其添加到LinkedList对象中 11 Student stu1 = new Student(1,"zhangsan",20); 12 linkedList.add(stu1); 13 14 //创建一个Student对象,并将其添加到LinkedList对象中 15 Student stu2 = new Student(2,"lisi",21); 16 linkedList.add(stu2); 17 18 //创建一个Student对象,并将其添加到LinkedList对象中 19 Student stu3 = new Student(3,"wangwu",22); 20 linkedList.add(stu3); 21 System.out.println("linkedList:" + linkedList); 22 23 System.out.println("linkedList.indexOf(stu2):" + linkedList.indexOf(stu2)); 24 25 Student stu4 = new Student(4,"erha",22); 26 System.out.println("linkedList.indexOf(stu4):" + linkedList.indexOf(stu4)); 27 } 28 } 29 30 运行结果: 31 linkedList:[Student [stuId=1, stuName=zhangsan, stuAge=20], Student [stuId=2, stuName=lisi, stuAge=21], Student [stuId=3, stuName=wangwu, stuAge=22]] 32 linkedList.indexOf(stu2):1 33 linkedList.indexOf(stu4):-1
源代码如下:
1 /* 2 查询元素o在此双端队列中的位置(如果不存在,则返回-1) 3 */ 4 public int indexOf(Object o) { 5 int index = 0; 6 if (o == null) { 7 //当元素o为null时 8 //循环遍历此双端队列中的每个元素,查找是否有元素为null 9 for (Node<E> x = first; x != null; x = x.next) { 10 if (x.item == null) 11 return index; 12 index++; 13 } 14 } else { 15 //当元素o不为null时 16 //循环遍历此双端队列中的每个元素,查找是否有元素的值等于o 17 for (Node<E> x = first; x != null; x = x.next) { 18 if (o.equals(x.item)) 19 return index; 20 index++; 21 } 22 } 23 return -1; 24 }
(2) int lastIndexOf(Object o)
功能: 查询元素o在此双端队列中最后一次出现的位置(如果不存在则返回-1)
示例代码:
1 import java.util.LinkedList; 2 3 public class LinkedListDemo { 4 public static void main(String[] args) { 5 /*********测试LinkedList的'int lastIndexOf(Object o)'方法的使用**********/ 6 7 //创建一个LinkedList对象 8 LinkedList<Student> linkedList = new LinkedList<Student>(); 9 10 //创建一个Student对象,并将其添加到LinkedList对象中 11 Student stu1 = new Student(1,"zhangsan",20); 12 linkedList.add(stu1); 13 14 //创建一个Student对象,并将其添加到LinkedList对象中 15 Student stu2 = new Student(2,"lisi",21); 16 linkedList.add(stu2); 17 18 //创建一个Student对象,并将其添加到LinkedList对象中 19 Student stu3 = new Student(3,"wangwu",22); 20 linkedList.add(stu3); 21 linkedList.add(stu2); 22 linkedList.add(stu3); 23 System.out.println("linkedList:" + linkedList); 24 25 System.out.println("linkedList.lastIndexOf(stu2):" + linkedList.lastIndexOf(stu2)); 26 27 Student stu4 = new Student(4,"erha",22); 28 System.out.println("linkedList.lastIndexOf(stu4):" + linkedList.lastIndexOf(stu4)); 29 } 30 } 31 32 运行结果: 33 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]] 34 linkedList.lastIndexOf(stu2):3 35 linkedList.lastIndexOf(stu4):-1
源代码如下:
1 /* 2 查询元素o在此双端队列中最后一次出现的位置(如果不存在,则返回-1) 3 */ 4 public int lastIndexOf(Object o) { 5 int index = size; 6 if (o == null) { 7 //当元素o为null时 8 //循环逆序遍历双端队列中每个节点元素,查询节点元素值为null的节点 9 for (Node<E> x = last; x != null; x = x.prev) { 10 index--; 11 if (x.item == null) 12 return index; 13 } 14 } else { 15 //当元素o为null时 16 //循环逆序遍历双端队列中每个节点元素,查询节点元素值等于元素o的节点 17 for (Node<E> x = last; x != null; x = x.prev) { 18 index--; 19 if (o.equals(x.item)) 20 return index; 21 } 22 } 23 return -1; 24 }
(3) boolean contains(Object o)
功能: 查询此双端队列中是否有元素o
示例代码:
1 import java.util.LinkedList; 2 3 public class LinkedListDemo { 4 public static void main(String[] args) { 5 /*********测试LinkedList的'boolean contains(Object o)'方法的使用**********/ 6 7 //创建一个LinkedList对象 8 LinkedList<Student> linkedList = new LinkedList<Student>(); 9 10 //创建一个Student对象,并将其添加到LinkedList对象中 11 Student stu1 = new Student(1,"zhangsan",20); 12 linkedList.add(stu1); 13 14 //创建一个Student对象,并将其添加到LinkedList对象中 15 Student stu2 = new Student(2,"lisi",21); 16 linkedList.add(stu2); 17 18 //创建一个Student对象,并将其添加到LinkedList对象中 19 Student stu3 = new Student(3,"wangwu",22); 20 linkedList.add(stu3); 21 linkedList.add(stu2); 22 linkedList.add(stu3); 23 System.out.println("linkedList:" + linkedList); 24 25 System.out.println("linkedList.contains(stu2):" + linkedList.contains(stu2)); 26 27 Student stu4 = new Student(4,"erha",22); 28 System.out.println("linkedList.contains(stu4):" + linkedList.contains(stu4)); 29 } 30 } 31 32 运行结果: 33 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]] 34 linkedList.contains(stu2):true 35 linkedList.contains(stu4):false
源代码如下:
1 public boolean contains(Object o) { 2 //调用indexOf方法返回元素o在此双端队列中的位置 3 return indexOf(o) != -1; 4 }
(4) int size()
功能: 返回此双端队列中元素的个数
示例代码:
1 import java.util.LinkedList; 2 3 public class LinkedListDemo { 4 public static void main(String[] args) { 5 /*********测试LinkedList的'int size()'方法的使用**********/ 6 7 //创建一个LinkedList对象 8 LinkedList<Student> linkedList = new LinkedList<Student>(); 9 10 //创建一个Student对象,并将其添加到LinkedList对象中 11 Student stu1 = new Student(1,"zhangsan",20); 12 linkedList.add(stu1); 13 14 //创建一个Student对象,并将其添加到LinkedList对象中 15 Student stu2 = new Student(2,"lisi",21); 16 linkedList.add(stu2); 17 18 //创建一个Student对象,并将其添加到LinkedList对象中 19 Student stu3 = new Student(3,"wangwu",22); 20 linkedList.add(stu3); 21 linkedList.add(stu2); 22 linkedList.add(stu3); 23 System.out.println("linkedList:" + linkedList); 24 25 System.out.println("linkedList.size():" + linkedList.size()); 26 } 27 } 28 29 运行结果: 30 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]] 31 linkedList.size():5
源代码如下:
1 public int size() { 2 //返回属性size 3 return size; 4 }
--------------------------------------------------------------------
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>
Java中的标记接口 迭代器模式 Java中的深拷贝和浅拷贝 java.util.Arrays
java.util.Queue<E> java.util.Deque<E>