写在前面的话:读书破万卷,编码如有神

--------------------------------------------------------------------

下文主要对java.util.LinkedList的4个查询操作进行介绍,主要内容包括:

1、LinkedList常用4个查询操作介绍

参考内容:

1、JDK源码(1.7)

--------------------------------------------------------------------
1、LinkedList常用4个查询操作介绍
(1) int indexOf(Object o)
功能: 查询元素o在此双端队列中的位置(如果不存在则返回-1)
示例代码:
1 importjava.util.LinkedList;2
3 public classLinkedListDemo {4 public static voidmain(String[] args) {5 /*********测试LinkedList的'int indexOf(Object o)'方法的使用**********/
6
7 //创建一个LinkedList对象
8 LinkedList linkedList = new LinkedList();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 intindexOf(Object o) {5 int index = 0;6 if (o == null) {7 //当元素o为null时8 //循环遍历此双端队列中的每个元素,查找是否有元素为null
9 for (Node x = first; x != null; x =x.next) {10 if (x.item == null)11 returnindex;12 index++;13 }14 } else{15 //当元素o不为null时16 //循环遍历此双端队列中的每个元素,查找是否有元素的值等于o
17 for (Node x = first; x != null; x =x.next) {18 if(o.equals(x.item))19 returnindex;20 index++;21 }22 }23 return -1;24 }
(2) int lastIndexOf(Object o)
功能: 查询元素o在此双端队列中最后一次出现的位置(如果不存在则返回-1)
示例代码:
1 importjava.util.LinkedList;2
3 public classLinkedListDemo {4 public static voidmain(String[] args) {5 /*********测试LinkedList的'int lastIndexOf(Object o)'方法的使用**********/
6
7 //创建一个LinkedList对象
8 LinkedList linkedList = new LinkedList();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 intlastIndexOf(Object o) {5 int index =size;6 if (o == null) {7 //当元素o为null时8 //循环逆序遍历双端队列中每个节点元素,查询节点元素值为null的节点
9 for (Node x = last; x != null; x =x.prev) {10 index--;11 if (x.item == null)12 returnindex;13 }14 } else{15 //当元素o为null时16 //循环逆序遍历双端队列中每个节点元素,查询节点元素值等于元素o的节点
17 for (Node x = last; x != null; x =x.prev) {18 index--;19 if(o.equals(x.item))20 returnindex;21 }22 }23 return -1;24 }
(3) boolean contains(Object o)
功能: 查询此双端队列中是否有元素o
示例代码:
1 importjava.util.LinkedList;2
3 public classLinkedListDemo {4 public static voidmain(String[] args) {5 /*********测试LinkedList的'boolean contains(Object o)'方法的使用**********/
6
7 //创建一个LinkedList对象
8 LinkedList linkedList = new LinkedList();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 booleancontains(Object o) {2 //调用indexOf方法返回元素o在此双端队列中的位置
3 return indexOf(o) != -1;4 }
(4) int size()
功能: 返回此双端队列中元素的个数
示例代码:
1 importjava.util.LinkedList;2
3 public classLinkedListDemo {4 public static voidmain(String[] args) {5 /*********测试LinkedList的'int size()'方法的使用**********/
6
7 //创建一个LinkedList对象
8 LinkedList linkedList = new LinkedList();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 intsize() {2 //返回属性size
3 returnsize;4 }
--------------------------------------------------------------------
java.util.LinkedList系列文章
--------------------------------------------------------------------