Java数组的简单实现

Java将数组封装为类的简单实现

public class Array {
    private long[] array;
    private int length = 0;

    //构造
    public Array(int size) {
        array = new long[size];
    }

    //插入,增加数组长度
    public void insert(long item) {
        array[length++] = item;
    }

//    查找
    public boolean find(long item) {
        for (int i = 0; i < length; i ++) {
            if (array[i] == item) {
                return true;
            }
        }
        return false;
    }

//    查找返回下标或-1
    public int indexOf(long item) {
        for (int i = 0; i < length; i ++) {
            if (array[i] == item) {
                return i;
            }
        }
        return -1;
    }

    //删除元素并移动数组
    public boolean delete(long item) {
        int i = 0;
        for (i = 0; i < length; i++) {
            if (item == array[i]) {
                break;
            }
        }
        if (i == length)
            return false;
        for (int j = i; j < length - 1; j++) {
            array[j] = array[j + 1];
        }
        length--;
        return true;
    }

//    打印数组
    public void show() {
        System.out.print("[");
        for (int i = 0; i < length; i++) {
            if (i == length -1)
                System.out.print(array[i]);
            else {
                System.out.print(array[i] + ", ");
            }
        }
        System.out.print("]\n");
    }

    public static void main(String[] args) {
        Array array = new Array(10);
        array.insert(1);
        array.insert(3);
        array.insert(2);
        array.show();
        System.out.println(array.length);
        int index = array.indexOf(2);
        if (array.find(2) && index != -1)
            System.out.println("找到了");
        else
            System.out.println("没找到");
        if (array.delete(2))
            System.out.println("删除了");
        else
            System.out.println("没找到");

    }
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java数组实现学生管理系统。以下是一个简单的示例,展示了如何创建学生类和管理系统类来存储和管理学生信息: ```java class Student { private String name; private int age; // 其他学生信息的属性 public Student(String name, int age) { this.name = name; this.age = age; } // 添加其他学生信息的方法 // 获取姓名 public String getName() { return name; } // 获取年龄 public int getAge() { return age; } } class StudentManagementSystem { private Student[] students; private int count; public StudentManagementSystem(int maxSize) { students = new Student[maxSize]; count = 0; } // 添加学生 public void addStudent(Student student) { if (count < students.length) { students[count] = student; count++; System.out.println("学生添加成功!"); } else { System.out.println("学生管理系统已满,无法添加更多学生!"); } } // 根据索引删除学生 public void removeStudent(int index) { if (index >= 0 && index < count) { for (int i = index; i < count - 1; i++) { students[i] = students[i + 1]; } count--; System.out.println("学生删除成功!"); } else { System.out.println("索引无效,无法删除学生!"); } } // 根据姓名查找学生 public void findStudentByName(String name) { boolean found = false; for (int i = 0; i < count; i++) { if (students[i].getName().equals(name)) { System.out.println("找到学生:" + students[i].getName()); found = true; } } if (!found) { System.out.println("未找到姓名为" + name + "的学生!"); } } // 获取学生数量 public int getStudentCount() { return count; } } public class Main { public static void main(String[] args) { StudentManagementSystem sms = new StudentManagementSystem(10); Student student1 = new Student("张三", 18); Student student2 = new Student("李四", 19); // 添加更多学生... sms.addStudent(student1); sms.addStudent(student2); sms.findStudentByName("张三"); System.out.println("学生数量:" + sms.getStudentCount()); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值