栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

Java学习笔记(十)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java学习笔记(十)

在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理Java知识点的第十篇博客。

本篇博客介绍了Java的ArrayList集合。

本系列博客所有Java代码都使用IntelliJ IDEA编译运行,版本为2022.1。所用JDK版本为JDK11。

目录

ArrayList

集合概述

ArrayList的构造和添加

ArrayList的常用方法

ArrayList存储字符串并遍历

ArrayList存储学生对象

简单的学生管理系统


ArrayList

集合概述

集合是一种存储空间可变的存储模型,存储的数据容量可以改变,集合类有很多,如ArrayList。

ArrayList 是一种可调整大小的数组实现,是泛型。在出现E的地方用数据类型替换即可。

ArrayList的构造和添加

使用ArrayList需要通过import java.util.ArrayList进行导包。

public ArrayList()创建一个空的集合对象。

public boolean add(E e)将指定的元素加入集合末尾,返回加入结果(成功为true,失败为false)。

public void add(int index,E e)在索引为index处插入e。索引不能超过目前元素个数。

import java.util.ArrayList;
public class arraylisttest1 {
    public static void main(String[] args){
        ArrayList test = new ArrayList();
        test.add("Hello ");
        test.add("Java ");
        test.add(2,"2022");
        System.out.println(test);
    }
}

程序创建了一个ArrayList集合,存储String字符串。

程序的输出是:

[Hello , Java , 2022]

ArrayList的常用方法

public boolean remove(Object o)删除指定元素,返回删除是否成功。

public E remove(int index)删除指定索引处的元素,返回被删除的元素。

public E set(int index,E e)修改指定索引的元素,并返回被修改的元素。

public E get(int index)返回指定索引处的元素。

public int size()返回集合中元素的个数。

import java.util.ArrayList;
public class arraylisttest2 {
    public static void main(String[] args){
        ArrayList test = new ArrayList ();
        test.add("Hello");
        test.add("world");
        test.add("hello");
        test.add("Java");
        System.out.println(test.set(2,"and"));
        System.out.println(test.remove(1));
        System.out.println(test.remove(1));
        System.out.println(test.get(1));
        System.out.println(test);
        System.out.println(test.size());
    }
}

程序的输出是:

hello
world
and
Java
[Hello, Java]
2

ArrayList存储字符串并遍历
import java.util.ArrayList;
public class arraylisttest3 {
    public static void main(String[] args){
        ArrayList test = new ArrayList ();
        test.add("Bonnie");
        test.add("Otto");
        test.add("Cesar");
        test.add("Joan");

        int i;
        for(i = 0; i < test.size();i+=1){
            System.out.println(test.get(i));
        }
    }
}

程序的输出是:

Bonnie
Otto
Cesar
Joan

ArrayList存储学生对象
public class studentarraylist {
    String name;
    int age;
    public studentarraylist(){}
    public studentarraylist(String name,int age){
        this.name = name;
        this.age = age;
    }
    public void setname(String name){
        this.name = name;
    }
    public String getname(){
        return name;
    }

    public void setage(int age){
        this.age = age;
    }
    public int getage(){
        return age;
    }
}

studentarraylist是一个学生类。

import java.util.Scanner;
import java.util.ArrayList;
public class studentarraylisttesst {
    public static void main(String[] args){
        ArrayList stus = new ArrayList();
        int i;
        for(i = 0; i <3; i += 1){
            getstud(stus);
        }

        for(i = 0;i < 3;i += 1){
            studentarraylist temp = new studentarraylist();
            temp = stus.get(i);
            System.out.println("The student's name is " + temp.getname());
            System.out.println("The student's age is " + temp.getage());
        }
    }
    public static void getstud(ArrayList stus){
        Scanner sc = new Scanner(System.in);
        studentarraylist temp = new studentarraylist();
        System.out.println("Please enter the name");
        String name = sc.nextLine();
        System.out.println("Please enter the age");
        int age = sc.nextInt();
        temp.setname(name);
        temp.setage(age);
        stus.add(temp);
    }
}

main方法中创建一个存储studentarraylist类对象的ArrayList集合。随后利用getstud方法添加三个学生对象,getstud方法中创建一个studentarraylist类对象,并进行输入,将结果赋给变量,最后将这个对象加入集合。回到main方法后,用一个对象接收成员,然后输出。

下面是一个运行示例

Please enter the name
Inigo
Please enter the age
19
Please enter the name
Monica
Please enter the age
16
Please enter the name
Olaf
Please enter the age
17
The student's name is Inigo
The student's age is 19
The student's name is Monica
The student's age is 16
The student's name is Olaf
The student's age is 17

简单的学生管理系统

可以利用ArrayList集合做一个简单的学生管理系统。

public class Studentsystem {
    private String name;
    private String id;
    private String age;
    private String address;

    public Studentsystem(){}
    public Studentsystem(String name, String id, String age, String address){
        this.name = name;
        this.id = id;
        this.age = age;
        this.address = address;
    }

    public void setname(String name){
        this.name = name;
    }
    public String getname(){
        return name;
    }

    public void setid(String id){
        this.id = id;
    }
    public String getid(){
        return id;
    }
    public void setage(String age){
        this.age = age;
    }
    public String getage(){
        return age;
    }

    public void setaddress(String address){
        this.address = address;
    }
    public String getaddress(){
        return address;
    }
}

这是系统用的学生类。

import java.util.Scanner;
import java.util.ArrayList;
public class Studentsystemuse {
    public static void main(String[] args){
        System.out.println("Welcome!");
        Scanner sc = new Scanner(System.in);
        int choice;
        ArrayList studentlist = new ArrayList();
        while(true){
            System.out.println("1. add");
            System.out.println("2. delete");
            System.out.println("3. revise");
            System.out.println("4. print");
            System.out.println("5. exit");
            System.out.println("Please enter the choice");
            choice = sc.nextInt();

            switch(choice){
                case 1:
                    add(studentlist);
                    break;
                case 2:
                    delete(studentlist);
                    break;
                case 3:
                    revise(studentlist);
                    break;
                case 4:
                    printf(studentlist);
                    break;
                case 5:
                    break;
                default:
                    System.out.println("Please enter again!");
                    break;
            }

            if(choice == 5){
                break;
            }
        }
        System.out.println("Bye!");
    }

    public static void add(ArrayList studentlist){
        Scanner sc = new Scanner(System.in);
        Studentsystem temp = new Studentsystem();

        String name;
        System.out.println("Please enter the name");
        name = sc.nextLine();
        temp.setname(name);

        String id;
        System.out.println("Please enter the id");
        id = sc.nextLine();
        temp.setid(id);
        String age;
        System.out.println("Please enter the age");
        age = sc.nextLine();
        temp.setage(age);

        String address;
        System.out.println("Please enter the address");
        address = sc.nextLine();
        temp.setaddress(address);

        studentlist.add(temp);
    }
    public static void printf(ArrayList studentlist){
        if(studentlist.size() == 0){
            System.out.println("It is blank");
            return;
        }
        int i;
        for(i = 0 ;i < studentlist.size() ;i += 1){
            Studentsystem temp = new Studentsystem();
            temp = (Studentsystem) studentlist.get(i);

            System.out.println("The name is " + temp.getname());
            System.out.println("The id is " + temp.getid());
            System.out.println("The age is " + temp.getage());
            System.out.println("The address is " + temp.getaddress());
            System.out.println();
        }
    }

    public static void delete(ArrayList studentlist){
        Scanner sc = new Scanner(System.in);
        String id;
        System.out.println("Please enter the id");
        id = sc.nextLine();

        Studentsystem temp = new Studentsystem();
        int i;
        boolean flag =false;
        for(i = 0; i < studentlist.size();i += 1){
            temp = (Studentsystem) studentlist.get(i);
            if(temp.getid().equals(id)){
                flag = true;
                studentlist.remove(i);
                System.out.println("Done");
                break;
            }
        }

        if(flag == false){
            System.out.println("Cannot find");
        }
    }

    public static void revise(ArrayList studentlist){
        Scanner sc = new Scanner(System.in);
        String originalid;
        System.out.println("Please enter the original id");
        originalid = sc.nextLine();
        Studentsystem temp = new Studentsystem();

        String name;
        System.out.println("Please enter the name");
        name = sc.nextLine();
        temp.setname(name);

        String id;
        System.out.println("Please enter the id");
        id = sc.nextLine();
        temp.setid(id);
        String age;
        System.out.println("Please enter the age");
        age = sc.nextLine();
        temp.setage(age);

        String address;
        System.out.println("Please enter the address");
        address = sc.nextLine();
        temp.setaddress(address);

        int i;
        boolean flag = false;
        for(i = 0;i< studentlist.size();i += 1){
            Studentsystem tmp = (Studentsystem) studentlist.get(i);
            if(tmp.getid().equals(originalid)){
                flag = true;
                studentlist.set(i,temp);
                System.out.println("Done");
                break;
            }
        }

        if(flag == false){
            System.out.println("Cannot found");
        }
    }
}

这段代码实现了简单的增删改查功能。这里不再显示运行结果。

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1040427.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号