java之介面

來源:互聯網
上載者:User

標籤:資料   span   bsp   知識   name   jic   ash   管理   隨機   

介面基礎知識

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());        }    }}

 

java之介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.