1. 程式人生 > 實用技巧 >JZOJ5405. 【NOIP2017提高A組模擬10.10】Permutation

JZOJ5405. 【NOIP2017提高A組模擬10.10】Permutation

技術標籤:javaarraylist

ArrayList:集合類的子類
是一個長度可變的容器。
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述

public static void main(String[] args) {
    ArrayList list = new ArrayList();
    // 新增元素
    list.add(1);
    list.add(true);
    list.add("hello");
    // 獲取list的元素的 個數
    System.out.println(list.size());

    //移除元素
    list.remove(
1); //修改元素 list.set(0,11); //遍歷ArrayList for(int i = 0 ; i < list.size();i++){ System.out.println(list.get(i)); } // 判斷容器中是否包含某一個元素 System.out.println(list.contains(111)); }

案例:儲存字串並遍歷
需求:建立一個儲存字串的集合,儲存3個字串元素,使用程式實現在控制檯遍歷該集合

import java.util.ArrayList;

public class
ArrayListDemo_02 { /* 案例:儲存字串並遍歷 需求:建立一個儲存字串的集合,儲存3個字串元素, 使用程式實現在控制檯遍歷該集合 思路: 1 建立一個 ArrayList 2 容器中儲存的是 String物件 3 遍歷 */ public static void main(String[] args) { // 建立一個容器 ArrayList<String> strList = new ArrayList<>
(); //新增元素 strList.add("java"); strList.add("HTML"); strList.add("Spring"); // 遍歷 for(int i = 0 ; i <strList.size();i++){ System.out.println(strList.get(i)); } // 使用增強for來遍歷 for(String str : strList){ System.out.println(str); } } }

案例:儲存學生物件並遍歷
需求:建立一個儲存學生物件的集合,儲存3個學生物件,使用程式實現在控制檯遍歷該集合
實現一:用陣列實現

public class Student {
    private int stuId;// 學號
    private String stuName;//姓名
    private int age;//年齡

    public Student() {
    }

    public Student(String stuName, int age) {
        this.stuName = stuName;
        this.age = age;
    }

    public Student(int stuId, String stuName, int age) {
        this.stuId = stuId;
        this.stuName = stuName;
        this.age = age;
    }

    public int getStuId() {
        return stuId;
    }

    public void setStuId(int stuId) {
        this.stuId = stuId;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public void show(){
        System.out.println(this.getStuId() +"--"+this.getStuName()+"---"+this.getAge());
    }
}


public class ArrayListDemo_03 {
    //使用陣列來儲存自定義型別
    public static void main(String[] args) {
        // 建立自定義型別的陣列
        Student[] stuArr = new Student[3];
        stuArr[0]= new Student(1,"張三",22);
        Student stu = new Student();
        stu.setStuId(2);
        stu.setStuName("李四");
        stu.setAge(20);
        stuArr[1] = stu;
        stuArr[2]= new Student(3,"王五",23);
        //遍歷陣列
        for(int i = 0 ; i < stuArr.length;i++){
            stuArr[i].show();
        }
        System.out.println("------------------------------");
        // 使用增強for
        for(Student student :stuArr){
            student.show();
        }
    }
}


在這裡插入圖片描述
實現方式二:ArrayList

import java.util.ArrayList;

public class ArrayListDemo_03 {
    //使用陣列來儲存自定義型別
    public static void main(String[] args) {
       
        //建立一個 容器
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student(1,"辛巴",21));
        Student student = new Student(2,"李佳琪",25);
        list.add(student);
        list.add(new Student(3,"維亞",28));
        for (int i = 0 ; i < list.size();i++){
            list.get(i).show();
        }
        System.out.println("------------------");
        for(Student stu1 :list){
            stu1.show();

        }
    }
}


案例:儲存學生物件並遍歷升級版
需求:建立一個儲存學生物件的集合,儲存3個學生物件,使用程式實現在控制檯遍歷該集合,學生的姓名和年齡來自於鍵盤錄入
思路:
①定義學生類,為了鍵盤錄入資料方便,把學生類中的成員變數都定義為String型別
②建立集合物件
③鍵盤錄入學生物件所需要的資料
④建立學生物件,把鍵盤錄入的資料賦值給學生物件的成員變數
⑤往集合中新增學生物件
⑥遍歷集合,採用通用遍歷格式實現

public class ArrayListDemo_04 {
    /*
    需求:建立一個儲存學生物件的集合,儲存3個學生物件,
    使用程式實現在控制檯遍歷該集合,
    學生的姓名和年齡來自於鍵盤錄入
     */
    public static void main(String[] args) {
        //1 建立集合
        ArrayList<Student> list = new ArrayList<>();
        //從鍵盤輸入 建立Scanner物件
        Scanner sc = new Scanner(System.in);
        // 2 構建學生物件
        for(int i = 0 ; i < 3 ;i++){
          // 每次都 建立 一個新的學生物件
            Student stu = new Student();
            //為學生物件的屬性賦值  輸入的時候 最好不要進行混合型別的輸入 
            System.out.println("請輸入學生的ID:");
            stu.setStuId(sc.nextLine());
            System.out.println("請輸入學生的姓名:");
            stu.setStuName(sc.nextLine());
            System.out.println("請輸入學生年齡:");
            stu.setAge(sc.nextLine());
            //將建立好的學生物件新增到集合
            list.add(stu);
        }
        //遍歷集合  輸出學生資訊
        for(Student stu : list){
           stu.show();
        }
    }
}