1 package com.mon11.day5;
2
3 import java.util.LinkedList;
4
5 /**
6 * 类说明 :
7 * @author 作者 : chenyanlong
8 * @version 创建时间:2017年11月5日
9 */
10 //定义学生类
11 class Student2{
12 String name;
13 int age;
14 public String getName() {
15 return name;
16 }
17 public int getAge() {
18 return age;
19 }
20
21 public Student2(String name,int age){
22 this.name=name;
23 this.age=age;
24 }
25
26 }
27
28 public class LinkedList1 {
29
30 public static void main(String[] args) {
31 //1.创建4个学生对象
32 Student2 student1=new Student2("tom1", 11);
33 Student2 student2=new Student2("tom2", 12);
34 Student2 student3=new Student2("tom3", 13);
35 Student2 student4=new Student2("tom4", 14);
36
37 //2.创建linkedList对象,把四个学生放入其中
38 LinkedList list=new LinkedList();
39 list.add(student1);
40 list.add(student2);
41 list.add(student3);
42 list.add(student4);
43
44 //3.查看第一个学生的信息
45 Student2 sFirst=(Student2) list.getFirst();
46 System.out.println("第一个学生的基本信息"+sFirst.name+"\t"+sFirst.getAge());
47
48 //4.查看第一个学生的信息
49 Student2 sLast=(Student2) list.getLast();
50 System.out.println("最后一个学生的基本信息"+sLast.name+"\t"+sLast.getAge());
51
52 //5.删除第一个学生,删除最后一个学生
53 list.removeFirst();
54 list.removeLast();
55
56 //6.显示剩余学生的信息
57 System.out.println("删除后学生的信息");
58 for(int i=0;i<list.size();i++){
59 Student2 student=(Student2) list.get(i);
60 System.out.println("最后一个学生的基本信息"+student.getName()+"\t"+student.getAge());
61 }
62 }
63 }


View Code


运行效果:

集合代码----小练习4_java