1. 栈

栈 就是一种容器 ,栈在存储数据的时候 ,有一个特点 ,先进后出

  • empty() 判断栈是不是 空的
  • Object peek() 查看栈顶数据 不会取出数据
  • Object pop() 删除栈顶数据 并返回
  • Object push(Object o) 把数据存储到栈顶 并返回此数据
  • int search(Object o) 查询 参数在栈中 距离 栈顶 的位置

自定义一个栈 :

  1. public class MyStack {
  2. int[] nums ; //声明一个数组
  3. int index ; //用于记录数组中的 元素个数
  4. //初始化栈对象
  5. public MyStack(){
  6. //默认数组容量是 16
  7. this(3);
  8. }
  9. private MyStack(int capacity){
  10. nums = new int[capacity];
  11. }
  12. //检查数组容量是否超标
  13. public void checkCapacity(){
  14. if(index == nums.length) {
  15. //扩容
  16. int[] newNums = new int[nums.length*2];
  17. //数组的拷贝
  18. System.arraycopy(nums, 0, newNums, 0, nums.length);
  19. nums = newNums ;
  20. /*for(int i=0;i<nums.length;i++){
  21. newNums[i] = nums[i];
  22. }
  23. nums = newNums;*/
  24. }
  25. }
  26. /**
  27. * 向栈中存储数据
  28. * @param num
  29. * @return
  30. */
  31. public int push(int num){
  32. //先检查容量
  33. checkCapacity();
  34. nums[index++] = num;
  35. return num ;
  36. }
  37. /**
  38. * 查看栈顶元素
  39. * @return
  40. */
  41. public int peek(){
  42. if(index == 0) {
  43. throw new RuntimeException("栈是空的,名义数据");
  44. }
  45. return nums[index-1];
  46. }
  47. /**
  48. * 判断栈是否是空的
  49. * @return
  50. */
  51. public boolean isEmpty(){
  52. return index == 0;
  53. }
  54. /**
  55. * 取出栈顶元素
  56. * @return
  57. */
  58. public int pop(){
  59. if(index == 0) {
  60. throw new RuntimeException("栈是空的,名义数据");
  61. }
  62. return nums[--index];
  63. }
  64. /**
  65. * 返回 num 距离栈顶的位置
  66. * @param num
  67. * @return
  68. */
  69. public int search(int num){
  70. int count = -1;
  71. for(int i = index-1 ;i>=0;i--){
  72. if(nums[i] == num){
  73. count = i ;
  74. }
  75. }
  76. if(count != -1) {
  77. return index - count;
  78. }
  79. return count ;
  80. }
  81. /**
  82. * 返回占中元素个数
  83. * @return
  84. */
  85. public int size(){
  86. return index ;
  87. }
  88. }

2. 队列

队列也是一种数据结构,也是一个容器,队列的数据结构 先进先出

  1. package com.woniu.day11.struct;
  2. import java.util.Queue;
  3. /**
  4. * add(Object o)
  5. * Object element();
  6. * offer(Object o)
  7. * peek()
  8. * Object poll()
  9. * Object remove()
  10. * @author Administrator
  11. */
  12. public class MyQueue {
  13. String[] names;
  14. int size ;
  15. int first ;
  16. int last;
  17. public MyQueue(){
  18. this(2);
  19. }
  20. public MyQueue(int capacity){
  21. names = new String[capacity];
  22. }
  23. /**
  24. * 检查数组是否越界
  25. * public static void arraycopy(
  26. * src - 源数组。
  27. srcPos - 源数组中的起始位置。
  28. dest - 目标数组。
  29. destPos - 目的地数据中的起始位置。
  30. length - 要复制的数组元素的数量。
  31. )
  32. */
  33. public void checkCapacity(){
  34. if(size == names.length){
  35. String[] newNames = new String[names.length*2];
  36. System.arraycopy(names, 0, newNames, 0, names.length);
  37. names = newNames ;
  38. }
  39. }
  40. /**
  41. * 返回队列的元素个数
  42. * @return
  43. */
  44. public int size(){
  45. return this.size ;
  46. }
  47. /**
  48. * 添加元素向队列
  49. * @param name
  50. */
  51. public void add(String name){
  52. checkCapacity(); //检查容量
  53. names[last++] = name;
  54. size++ ;
  55. }
  56. /**
  57. * 判断是否有数据
  58. * @return
  59. */
  60. public boolean isEmpty(){
  61. return size == 0;
  62. }
  63. /**
  64. * 查看尾部元素
  65. * @return
  66. */
  67. public String element(){
  68. if(size == 0){
  69. return null;
  70. }
  71. return names[first];
  72. }
  73. /**
  74. * 查看尾部元素
  75. * @return
  76. */
  77. public String peek(){
  78. if(size == 0){
  79. return null;
  80. }
  81. return names[first];
  82. }
  83. /**
  84. * 取出尾部元素
  85. * @return
  86. */
  87. public String poll(){
  88. if(size == 0){
  89. return null;
  90. }
  91. if(first <= size -1){
  92. String name = names[first];
  93. names[first] = null;
  94. first++ ;
  95. size--;
  96. return name;
  97. }else {
  98. return null;
  99. }
  100. }
  101. /**
  102. * 删除尾部元素
  103. * @return
  104. */
  105. public String remove(){
  106. if(size == 0){
  107. return null;
  108. }
  109. if(first <= size-1) {
  110. String name = names[first];
  111. names[first] = null;
  112. first++ ;
  113. return name ;
  114. }else {
  115. return null;
  116. }
  117. }
  118. }

3. 利用数组实现一个动态数组

在数组中任意 添加 、修改、删除 、搜索

我有一个学生对象 ,向数组中 任意的操作 学生对象

学生属性 :学号 、姓名、年龄、性别

创建一个学生类 :

public class Student {
    private String snum ;
    private String sname ;
    private int age ;
    private String sex ;

    public String getSnum() {
        return snum;
    }
    public void setSnum(String snum) {
        this.snum = snum;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Student(String snum, String sname, int age, String sex) {
        super();
        this.snum = snum;
        this.sname = sname;
        this.age = age;
        this.sex = sex;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + ((sex == null) ? 0 : sex.hashCode());
        result = prime * result + ((sname == null) ? 0 : sname.hashCode());
        result = prime * result + ((snum == null) ? 0 : snum.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Student other = (Student) obj;
        if (age != other.age)
            return false;
        if (sex == null) {
            if (other.sex != null)
                return false;
        } else if (!sex.equals(other.sex))
            return false;
        if (sname == null) {
            if (other.sname != null)
                return false;
        } else if (!sname.equals(other.sname))
            return false;
        if (snum == null) {
            if (other.snum != null)
                return false;
        } else if (!snum.equals(other.snum))
            return false;
        return true;
    }
    @Override
    public String toString() {
        return snum +"\t" + sname +"\t"+ age + "\t" +sex ;
    }
}

创建一个 动态数组:

package com.woniu.day11.stu;
/**
 * 操作学生对象 
 * @author Administrator
 *
 */
public class StuList {
    public static final int DEFAULT_CAPACITY = 16;
    Student[] stus ;
    int size ;

    public StuList(){
        this(DEFAULT_CAPACITY);
    }

    public StuList(int capacity){
        stus = new Student[capacity];
    }

    /**
     * 查询数组有多少学生对象
     * @return
     */
    public int size(){
        return this.size ;
    }

    /**
     * 判断数组中是否有元素
     * @return
     */
    public boolean isEmpty(){
        return size == 0;
    }

    /**
     * 判断是否越界
     */
    public void checkCapacity(){
        if(size == stus.length) {
            Student[] newStus = new Student[stus.length*2];
            System.arraycopy(stus, 0, newStus, 0, stus.length);
            stus = newStus ;
        }
    }

    /**
     * 向数组汇总添加一个元素
     * @param stu
     * @return
     */
    public Student add(Student stu){
        //判断容量
        checkCapacity();
        //存储到数组中
        stus[size++] = stu;
        return stu ;
    }

    /**
     * 根据下标返回学生对象
     * @param index
     * @return
     */
    public Student get(int index){
        if(index < 0 || index >= size){
            return null;
        }
        return stus[index];
    }

    /**
     * 返回 某一个学生对象 在数组中的位置
     * @param stu
     * @return  -1 代表 数组中没有此对象
     */
    public int indexOf(Student stu){
        for(int i=0;i<size ;i++){
            if(stus[i].equals(stu)) {
                return i ;
            }
        }
        return -1 ;
    }
    /**
     * 替换 index 位置的 学生对象
     * @param index
     * @param stu
     * @return  返回原来的对象
     */
    public Student modify(int index ,Student stu){
        if(index < 0 || index >= size){
            return  null ;
        }

        Student oldStu = stus[index];
        stus[index] = stu ;

        return oldStu ;
    }

    /**
     * 根据下标删除 一个元素
     * @param index
     * @return
     */
    public Student delete(int index){
        if(index < 0 || index >= size){
            return  null ;
        }

        Student deleteStu = stus[index] ;
        //数组拷贝
        System.arraycopy(stus ,index+1 ,stus ,index ,size-index-1);
        size-- ;
        return deleteStu ;
    }
    /**
     * 根据对象删除
     * @param stu
     */
    public Student remove(Student stu){
        int indexOf = this.indexOf(stu);
        return this.delete(indexOf);
    }

    /**
     * 在制定位置 添加 元素
     * @param index
     * @param stu
     * @return
     */
    public Student addIndex(int index ,Student stu){
        checkCapacity();

        if(index >= size){
            stus[size++] = stu;
        }

        //数组拷贝 
        System.arraycopy(  stus ,index ,stus ,index+1 ,size - index );
        size++ ;
        stus[index] = stu;
        return stu ;
    }

    /**
     * 返回所有的学生对象
     * @return
     */
    public Student[] getAll(){
        if(size == 0){
            return null;
        }
        Student[] my = new Student[size];
        System.arraycopy(stus, 0, my, 0, size);
        return my ;
    }
}

MainApp :

package com.woniu.day11.stu;

import java.util.Scanner;

public class MainApp {
    //创建一个 动态数组对象
    static StuList list = new StuList();
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {

        while(true){
            initMenu();//显示菜单
            System.out.println("请输入操作:");
            String op = sc.next();

            switch (op) {
            case "1":
                search();
                break;
            case "2":
                add();
                break;
            case "3":
                modify();
                break;
            case "4":
                del();
                break;
            case "0":
                System.out.println("Bye,Bye!!!");
                System.exit(0);//退出虚拟机
                break;

            default:
                System.out.println("输入有误,请从新输入.");
                break;
            }
        }
    }

    static void modify(){
        System.out.println("请输入修改的学生的学号:");
        String snum = sc.next();

        Student[] all = list.getAll();
        if(all != null){
            Student stu = null;
            int index = -1;
            for(int i=0;i<all.length;i++){
                if(all[i].getSnum().equals(snum)){
                    stu = all[i] ;
                    index = i ;
                    break ;
                }
            }

            //判断是否存在改学生
            if(stu != null){
                System.out.println("请输入姓名:");
                String sname = sc.next();
                System.out.println("请输入年龄:");
                int age = sc.nextInt();
                sc.nextLine();// 接收回车
                System.out.println("请输入性别:");
                String sex = sc.next();
                //重新赋值  姓名 、年龄、性别
                stu.setAge(age);
                stu.setSex(sex);
                stu.setSname(sname);

                list.modify(index, stu);

                System.out.println("修改成功!");

            }else {
                System.out.println("没有此学生,无法修改!");
            }
        }else {
            System.out.println("数据不存在,无法修改!!!");
        }
    }

    static void del(){
        System.out.println("请输入删除的学生学号:");
        String snum = sc.next();

        Student[] all = list.getAll();
        if(all != null) {
            boolean  f = true ;
            for(Student s :all){
                if(s.getSnum().equals(snum)){
                    list.remove(s);
                    f = false ;
                    break ;
                }
            }

            if(f) {
                System.out.println("没有此学号,无法删除!");
            }
        }else {
            System.out.println("数据不存在,无法删除");
        }

    }

    static void add(){
        System.out.println("请输入学号:");
        String snum = sc.next();
        System.out.println("请输入姓名:");
        String sname = sc.next();
        System.out.println("请输入年龄:");
        int age = sc.nextInt();
        sc.nextLine();// 接收回车
        System.out.println("请输入性别:");
        String sex = sc.next();

        Student s = new Student(snum, sname, age, sex);
        list.add(s);

        System.out.println("添加成功:"+s);
    }

    static void search(){
        Student[] students = list.getAll();
        if(students != null){
            System.out.println("学号\t姓名\t年龄\t性别");
            for(Student s :students){
                System.out.println(s);
            }
        }else {
            System.out.println("没有学生信息!!!");
        }
    }

    static void initMenu(){
        System.out.println("***********蜗牛学生管理系统************");
        System.out.println("           1.查询                                                 ");
        System.out.println("           2.添加                                                 ");
        System.out.println("           3.修改                                                 ");
        System.out.println("           4.删除                                                 ");
        System.out.println("           0.退出                                                 ");
    }
}