集合的笼统介绍之ArrayList

前文学习了数组和集合之后,我们知道数组是不可变的,集合是可变的,并且存储的是引用数据类型,它们都是容器。
与数组的存储遍历相同,集合也是①新建集合②存储③遍历,以此对集合进行各种增删改查。

自定义类型:

 

//封装两个变量到phone对象中
        phone p=new phone();//具有所有手机的方法和属性 Scanner 是一个功能,
        //调用方法,可以重复调用,而phone调取属性需要多次重复命名
        p.color="绿";                    //类是抽象的,不能直接描述对象,只能创建一个新的对象来描述对象的方法和属性,
        p.size =5.5;//因为创建一个新的对象所以在新对象中描述对象的方法和属性 进入堆
        System.out.println(p.color);
        System.out.println(p.size);
        System.out.println(p.brand);
        //变量名不一样,所以没有关系
        phone p2=new phone();
        p2.color="骚气粉";
        p2.size=4.7;
        p2.brand="小米";
        System.out.println(p2.color);
        System.out.println(p2.size);
        System.out.println(p2.brand);

自定义类型流程图:
集合的流程:
导包:import java.util.ArrayList;
创建类型:ArrayList<要存储元素的数据类型> 变量名 = new ArrayList<要存储元素的数据类型>();集合存储的数据类型必须都是引用数据类型。

基本数据类型转换为引用数据类型

基本数据类型

对应的引用数据类型表示形式

byte

Byte

short

Short

int

Integer

long

Long

float

Float

double

Double

char

Character

boolean

Boolean



集合的常用方法:

 

方法声明

功能描述

boolean addObject obj 返回值是布尔

将指定元素obj追加到集合的末尾

Object getint index

返回集合中指定位置上的元素

int size()

返回集合中的元素个数

当我们看到这三个集合常用方法,新增add(),get()获取元素,size()长度之后,就可以进行基本遍历了。

因为存储基本类型比较熟悉,我们来复习一下存储自定义类Student

student类,

public class Student {
    private String name;
    private String sex;
    
    public Student(String name, String sex) {
        super();
        this.name = name;
        this.sex = sex;
    }//下面还需要选出getxxx,setxxx,tostring

测试类:

public static void main(String[] args) {
        ArrayList<Person> list =new ArrayList<Person>();
        //因为person类里面是构造方法,所以这边直接new对象传递参数
        list.add(new Person("司南",22));
        list.add(new Person("周戎",25));
        //遍历,集合的长度
        for(int i=0;i<list.size();i++){
            System.out.println(list.get(i));
        }

效果:

方法声明

功能描述

boolean addint index,  Object obj方法重载

将指定元素obj插入到集合中指定的位置

Object removeint index

从集合中删除指定index处的元素,返回该元素

void clear()

清空集合中所有元素

Object setint index, Object obj

用指定元素obj替代集合中指定位置上的元素

其余方法的操作:

 

ArrayList<String> list=new ArrayList<String>();//泛型  对象容器进堆
        list.add("萌萌哒");
        list.add("么么哒");
        //在指定位置添加元素,在下标为1的地方进行添加,方法重载
        list.add(0,"司南");
        //删除指定元素 object具有返回值
        list.remove(0);
        //修改指定位置上的值 object具有返回值
        list.set(0,"周戎");
        //清空集合
        //list.clear();

 

案例介绍:用ArrayList+方法来写超级管理系统。

自定义类:

package com.oracle.demo03;

public class Shop {
//封装
//变量
    private int number;存储物品编号
    private String name;存储物品名称
    private double price;存储物品价格
//方法
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    
}

测试类:

package com.oracle.demo03;

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

public class Cs_Shop {
    public static void main(String[] args) {
        //创建新类
    
        Scanner  sc =new Scanner(System.in);
        //创建新集合
        ArrayList<Shop> list=new ArrayList<Shop>();
        
        Shop shop =new Shop();
        shop.setNumber(9001);
        shop.setName("少林寺");
        shop.setPrice(17.0);
        list.add(shop);
        Shop shop1 =new Shop();
        shop1.setNumber(9002);
        shop1.setName("少林寺");
        shop1.setPrice(17.0);
        list.add(shop1);
        Shop shop2 =new Shop();
        shop2.setNumber(9003);
        shop2.setName("少林寺");
        shop2.setPrice(17.0);
        list.add(shop2);
        
        while(true){
            show();
            int choose=sc.nextInt();
            switch(choose){
            case 1:getShop(list);
                break;
            case 2:addShop(list);
                break;
            case 3:updateShop(list);
                break;
            case 4:deleteShop(list);
                break;
            case 5:
                return;
            }
        }
    }
    //菜单
    public static void show(){
        
        System.out.println("======================欢迎光临oracle超市====================");
        System.out.println("1.货物清单");
        System.out.println("2.添加货物");
        System.out.println("3.修改货物");
        System.out.println("4.删除货物");
        System.out.println("5.退出");
        System.out.println("请输入您的选择: ");
        
    }
    //添加
    public static void addShop(ArrayList<Shop> list){
    Shop shop1=new Shop();
    Scanner sc=new Scanner(System.in);
    System.out.println("请输入编号");
    int num=sc.nextInt();
    shop1.setNumber(num);
    System.out.println("请输入名称");
    String new_name=sc.next();
    shop1.setName(new_name);
    System.out.println("请输入价格");
    double new_price=sc.nextDouble();
    shop1.setPrice(new_price);
    list.add(shop1);
    System.out.println("添加完成");
    }
    //查看
    public static void getShop(ArrayList<Shop> list){
        //Shop shop=new Shop();
        System.out.println("商品编号\t\t商品名称\t\t商品价格");
        for(int i=0;i<list.size();i++){
            System.out.println(list.get(i).getNumber()+"\t\t"+
        list.get(i).getName()+"\t\t"+list.get(i).getPrice());
        }
    }
    //删除
    public static void deleteShop(ArrayList<Shop> list){
        //调取查看
        getShop(list);
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入编号");
        int delete1=sc.nextInt();
        //遍历数组
        for(int i=0;i<list.size();i++){
            if(list.get(i).getNumber()==delete1){
                list.remove(i);
            }
        }
        System.out.println("删除完毕");
        
    }
    //修改
    public static void updateShop(ArrayList<Shop> list){
        getShop(list);
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要修改的编号");
        int update1=sc.nextInt();
        System.out.println("请输入要修改的名称");
        String  update2=sc.next();
        System.out.println("请输入要修改的价格");
        double update3=sc.nextDouble();
        for(int i=0;i<list.size();i++){
            if(list.get(i).getNumber()==update1){
                list.get(i).setName(update2);
                list.get(i).setPrice(update3);
            }
        }
    }
}

效果:

 

posted @ 2019-08-14 16:27  lvyimin  阅读(180)  评论(0编辑  收藏  举报