java之接口

接口基础知识

package ch09;

public class jichu {
    //接口:一组规范,一个标准
        //COLLECTION接口家族:list和set
            //collection 接口存储一组不唯一,无序de对象
                //LIst接口存储一组不唯一 有序(插入顺序)的对象
                    //ArrayList:实现了长度可变的数组,在内存中分配连续的空间,遍历元素和随机访问元素的效率比较高
                    //LinkedList:采用链表存储方式。插入、删除元素时效率比较高(不常用)
                //Set接口存储一组唯一,无序的对象
                    //HashSet
                    //TreeSet
        //MAP接口家族
            //Map接口存储一组键值对象,提供key到value的映射
                //HashMap
                //TreeMap
}

 

 

===========================================================================================

详细基础知识重点

package ch09;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;

public class Test {
    public static void main(String[] args) {
        // 创建一个集合,长度可变的数组(默认长度为10)
        List<Object> list = new ArrayList<Object>();
        // 使用集合对数据进行管理操作
        // 1:存数据
        list.add("a");
        list.add("b");
        // 2:取
        System.out.println(list.get(0));
        // 3:获取集合的长度
        System.out.println(list.size());
        // 4:删除
        list.remove(0);
        /****************************** 练习 ***********************************/
        /*
         * 定义一个学生类,包含学号,姓名,年龄 定义一个list集合,用于保存学员信息 数据不少于5个
         */
        list.add(new Student("jredu", "zhang", 12));
        list.add(new Student("jrwedu", "zang", 123));

        // 遍历数组
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) instanceof Student) {
                Student s = (Student) list.get(i);
                System.out.println(s.getName());
            }
        }

        // 三种遍历方式
        System.out.println("*******************泛型**********************");
        /*
         * 泛型集合:指定集合存储的数据类型 和数组类似 
         */
        List<Student> students = new ArrayList<Student>();
        students.add(new Student("jredu", "zhang", 12));
        students.add(new Student("jredu2", "zhang2", 122));
        // 遍历1
        for (int i = 0; i < students.size(); i++) {
            Student s = (Student) students.get(i);
            System.out.println(s.getNo());
        }
        // 遍历2
        System.out.println("***************foreach*****************");
        for (Student s : students) {
            System.out.println(s.getNo());
        }
        // 遍历3:迭代器
        System.out.println("***************迭代器*****************");
        Iterator<Student> it = students.iterator();
        while (it.hasNext()) {
            Student s = it.next();
            System.out.println(s.getNo());
        }

        /**********************************/
        System.out.println("*********LinkedList**************");
        LinkedList<Student> lnk = new LinkedList<Student>();
        // 1、添加一个集合的数据进来
        lnk.addAll(students);
        for (Student s : lnk) {
            System.out.println(s.getName());
        }
        System.out.println("******************************");
        // 2、在头部添加一个学生
        lnk.addFirst(new Student("jredu003", "wang5", 24));
        for (Student s : lnk) {
            System.out.println(s.getName());
        }

    }
}

 

 

=====================================================================================

例题

package ch09;

public class Student {
    private String no;
    private String name;
    private int age;

    public Student() {
        super();
    }

    public Student(String no, String name, int age) {
        super();
        this.no = no;
        this.name = name;
        this.age = age;
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

 

package ch09;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StudentTest {
    public static void main(String[] args) {

        List<Student> list = new ArrayList<Student>();
        Student[] s = new Student[5];
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < 5; i++) {
            System.out.println("请输入学号");
            String num = input.next();
            System.out.println("请输入名字");
            String name = input.next();
            System.out.println("请输入年龄");
            int age = input.nextInt();
            s[i] = new Student(num, name, age);
            list.add(s[i]);
        }
        for (int i = 0; i < 5; i++) {
            Student a = (Student) list.get(i);
            System.out.println(a.getNo() + a.getName() + a.getAge());

        }
    }
}

 

posted @ 2017-09-06 12:30  哈喽杏红  阅读(130)  评论(0编辑  收藏  举报