学Java的第十一天 | ArrayList集合 | 案例-简易学生管理系统 | Collection接口 | 案例-双色球开奖号码 |

1 ArrayList集合

1. 构造方法

public ArrayLIst()创建一个空的集合
public boolean add(E e)添加对象到集合中
public void add(int index , E e)添加对象到指定位置
//创建集合对象
ArrayList<String> arrayList = new ArrayList<>();
//向集合中添加对象
arrayList.add("Harmon");
arrayList.add("Jolin");
arrayList.add("Harry");
arrayList.add("Benny");
//向指定的位置添加对象  *在指定位置添加对象的时候要注意下表越界问题*
arrayList.add(4,"Borgv");
System.out.println(arrayList);

2. 常用方法

public boolean remove(object o)删除对象成功返回true
public E remove(int index)删除指定位置的元素返回被删除的元素
public E set(int index , E e)在指定位置设置值(改变值)返回被修改的那个值
public E get(itn index , E e)获取指定位置的值返回被获取的值
public int size()获取集合的大小返回集合大小
public class Test01 {
    public static void main(String[] args) {
        //创建集合对象
        ArrayList<String> arrayList = new ArrayList<>();
        //向集合中添加对象
        arrayList.add("Harmon");
        arrayList.add("Jolin");
        arrayList.add("Harry");
        arrayList.add("Benny");
        //向指定的位置添加对象
        arrayList.add(4,"Borgv");
        //输出集合
        System.out.println(arrayList);//[Harmon, Jolin, Harry, Benny, Borgv]
        //删除元素
        System.out.println(arrayList.remove("Borgv"));//true
        //删除指定位置的元素
        System.out.println(arrayList.remove(3));//Benny
        //改变值
        System.out.println(arrayList.set(0,"Elizabeth"));//Harmon
        //获取集合大小
        System.out.println(arrayList.size());//3
        //获取值
        for(int i = 0;i < arrayList.size() ;i++){
            System.out.print(arrayList.get(i) + " ");
        }//Elizabeth Jolin Harry 
    }
}

2 案例-简易版的学生管理系统

student类:

public class Student {
    private String name ;
    private Integer age ;
    private String sex ;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Student(String name, Integer age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
}

测试类:

public class StuManger {
    public static void main(String[] args) {
        //创建学生集合students
        ArrayList<Student> students = new ArrayList<>();
        //初始化学生集合 把创建的集合参数传入方法 添加一些学生对象
        initStuinfo(students);
        //建立一个死循环
        while (true){
            //功能调度器  传入的参数1来调动对应功能 传入的参数2是学生集合
            funAction(showWelcomePage(),students);
        }
    }

    //功能调度器
    private static void funAction(int a,ArrayList<Student> students) {
        switch (a){
            case 1:
                System.out.println("** showStudentinfomation    ***********");
                //这里将学生集合传进浏览学生信息方法中使用
                showStuList(students);
                break;
            case 2 :
                System.out.println("** addStudentinformation    ***********");
                //这里实际上是一个死循环  addStu做成一个返回boolean值得方法 用于添加学生对象
                //同样的把学生集合传入添加学生信息方法使用
                while (addStu(students));
                break;
            case 3 :
                System.out.println("** reviseStudentinfomation  ***********");
                break;
            case 4 :
                System.out.println("** deleteStudentinfomation  ***********");
                break;
            case 0 :
                System.out.println("** Have a nice day!    ****************");
                System.exit(0);
                break;



        }
    }

    //添加学生信息的方法
    private static boolean addStu(ArrayList<Student> students) {
        //创建一个学生对象用于接收用户传进来的学生信息
        Student student = getStudentPage();
        //如果用户在getStudentPage中不想再添加学生数据的时候传入exit 就会返回一个为null值的student
        if (null == student ){
            //如果是null 返回false 在功能调度器中就不会再循环
            return false;
        }else {
            //否则 返回添加学生数据的结果-->true
            return students.add(student);
        }
    }

    //从用户中获取想要添加的学生信息的方法  返回值是学生类
    private static Student getStudentPage() {
        //建立一个学生对象
        Student student = null;
        System.out.println("** Please input your name  :          *");
        String name = new Scanner(System.in).nextLine();
        //如果用户传入exit  返回的就是null值
        if (name.equals("exit"))
            return student;
        System.out.println("** Please input your sex   :          *");
        String sex = new Scanner(System.in).nextLine();
        System.out.println("** Please input your age   :          *");
        int age = new Scanner(System.in).nextInt();

        //使用这个学生对象来接受用户传入的想添加的学生信息
        student = new Student(name,age,sex);
        //返回对象
        return student;
    }

    //浏览学生信息的方法
    private static void showStuList(ArrayList<Student> students) {
        System.out.println("┌─────────────┬──────┬──────┐");
        System.out.printf("│%13s│%6s│%6s│\n","name","sex","age");
        for (Student student:students) {
            System.out.println("├─────────────┼──────┼──────┤");
            System.out.printf("│%13s│%6s│%6d│\n",student.getName(),student.getSex(),student.getAge());
        }
        System.out.println("└─────────────┴──────┴──────┘");
    }

    //初始欢迎界面 返回一个int类型的值
    private static int showWelcomePage() {
        System.out.println("***************************************");
        System.out.println("** Welcome to Student Manager System **");
        System.out.println("** 1  showStudentinfo       ***********");
        System.out.println("** 2  addNewStudentinfo     ***********");
        System.out.println("** 3  reviseStudentinfo     ***********");
        System.out.println("** 4  deletewStudentinfo    ***********");
        System.out.println("** 0  exit                  ***********");
        System.out.println("***************************************");

        //定义一个值
        int a ;
        //如果用户传入了01234以外的值会一直让你输入正确的值:
        do {
            System.out.println("** Please input your choice:         **");
            //利用a 来接受用户的选择
            a = new Scanner(System.in).nextInt();
        }while (a>4||a<0);
        //返回这个值 以便功能调度器的分支语句执行
        return a;
    }

    //初始化学生集合的数据  传入一些原本就在里面的值
    private static void initStuinfo(ArrayList<Student> students) {
        students.add(new Student("Harmon",9,"girl"));
        students.add(new Student("Jolin",13,"girl"));
        students.add(new Student("Harry",20,"boy"));
        students.add(new Student("Benny",25,"boy"));
        students.add(new Student("Borgv",38,"boy"));
    }
}

3 Collection接口

1 概述
Collection是单项集合的顶层接口,他表示一组对象,这些对象被称之为Collection元素
Collection不能直接拿来使用,因为它是接口,所以需要通过子类接口的实现类来对其进行访问

package Collection;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @ClassName Test01$
 * @Description:
 * @Time 2021/3/1$ 15:24$
 * @Author-Email FONG_y@outlook.com
 */
public class Test01 {
    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        
        collection.add("apple");
        collection.add("orange");
        collection.add("strawberry");
        collection.add("watermelon");

        System.out.println(collection);
    }
}

2 常用方法

方法名说明
boolean add(E e)添加元素 成功返回true
boolean remove(E e)移除元素 成功赶回true
void clear()清空集合
boolean contains(Object o)集合中是否存在指定元素 存在返回true
boolean isEmpty()判断集合是否为空
int size()返回集合的大小
package Collection;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @ClassName Test02$
 * @Description:
 * @Time 2021/3/1$ 15:32$
 * @Author-Email FONG_y@outlook.com
 */
public class Test02 {
    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();

        collection.add("apple");
        collection.add("orange");
        collection.add("strawberry");
        collection.add("watermelon");

        System.out.println(collection);
        System.out.println(collection.size());//4

        System.out.println(collection.add("blackberry"));//添加成功 true
        System.out.println(collection.remove("orange"));//存在 且 移除 ==>  true
        System.out.println(collection.remove("orange"));//不存在 也没得移除 ==> false
        System.out.println(collection.contains("strawberry"));//true
        collection.clear();
        System.out.println(collection.isEmpty());//true
    }
}

3 遍历方式

package Collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @ClassName Test05$
 * @Description: Collection遍历方法
 * @Time 2021/3/2$ 21:58$
 * @Author-Email FONG_y@outlook.com
 */
public class Test05 {
    public static void main(String[] args) {

        Collection<String> collection = new ArrayList<>();
        collection.add("apple");
        collection.add("orange");
        collection.add("strawberry");
        collection.add("watermelon");

        //for循环增强遍历
        for (String string : collection) {
            System.out.println(string);
        }

        System.out.println("==========================");

        //通过迭代器遍历,迭代器不是集合本身
        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}

4.1 案例-双色球开奖号码-Collection

package Collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;
import java.util.Scanner;

/**
 * @ClassName Test03$
 * @Description:
 * @Time 2021/3/1$ 15:55$
 * @Author-Email FONG_y@outlook.com
 */
public class Test03 {
    public static void main(String[] args) {
        System.out.println("请输入要生成的注数 : ");
        int count = new Scanner(System.in).nextInt();
        for (int i = 0; i < count; i++) {
            getNumbers(i + 1);
        }
    }

    private static void getNumbers(int i) {
        Random random = new Random();
        Collection<Integer> Redball = new ArrayList<>();

        int Blueball = random.nextInt(16) + 1;

        while (Redball.size() < 6){
            int t = random.nextInt(33) + 1;
            if (!Redball.contains(t)){
                Redball.add(t);
            }
        }

        System.out.println("第" + i + "注 ===> 红球 :" + Redball + "\t蓝球 :" + Blueball);
    }
}

4.2 案例2-双色球开奖号码-数组

package Collection;

import java.util.Random;
import java.util.Scanner;

/**
 * @ClassName Test04$
 * @Description: 双色球开奖案例——数组
 * @Time 2021/3/2$ 21:16$
 * @Author-Email FONG_y@outlook.com
 */
public class Test04 {
    public static void main(String[] args) {
        System.out.println("请输入要生成的注数 :");
        int count = new Scanner(System.in).nextInt();
        for (int i = 0; i < count; i++) {
            getNumbers(i + 1);
        }
    }

    private static void getNumbers(int ii) {
        Random random = new Random();
        int[] redBall = new int[6];

        //蓝色球的范围是(1,16)
        int blueBall = random.nextInt(16)+1;

        for (int i = 0,j = 0; i < 6; i++) {
            //红色球的范围是(1,33)
            int t = random.nextInt(33)+1;

            //找相同的元素
            for (j = 0; j < i; j++) {

                if (t == redBall[j]){
                    //如果有相同的元素,i--,在外循环结束后,会进行i++,
                    //相当于索引没有变过,这个相同的数也不能被添加进来
                    i--;
                    break;
                }
            }
            //如果上面的循环是因为有相同的数退出来的,那么j符合循环中的j<i
            //如果是执行完循环条件退出,也就是没有相同的数的时候,就把这个数填入数组
            if (j >= i){
                redBall[i] = t ;
            }
        }

        //进行红球的排序
        for (int j = 0; j < redBall.length - 1 ; j++) {
            for (int k = 0; k < redBall.length - 1 - j ; k++) {
                if (redBall[k]>redBall[k+1]){
                    redBall[k]^=redBall[k+1];
                    redBall[k+1]^=redBall[k];
                    redBall[k]^=redBall[k+1];
                }
            }
        }

        System.out.printf("第%02d注 ===> 红球:",ii);
        for (int i = 0; i < 6; i++) {
            System.out.printf("  %02d",redBall[i]);
        }
        System.out.printf("  蓝球:%02d \n",blueBall);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值