1. 栈
栈 就是一种容器 ,栈在存储数据的时候 ,有一个特点 ,先进后出
- empty() 判断栈是不是 空的
- Object peek() 查看栈顶数据 不会取出数据
- Object pop() 删除栈顶数据 并返回
- Object push(Object o) 把数据存储到栈顶 并返回此数据
- int search(Object o) 查询 参数在栈中 距离 栈顶 的位置
自定义一个栈 :
public class MyStack {int[] nums ; //声明一个数组int index ; //用于记录数组中的 元素个数//初始化栈对象public MyStack(){//默认数组容量是 16this(3);}private MyStack(int capacity){nums = new int[capacity];}//检查数组容量是否超标public void checkCapacity(){if(index == nums.length) {//扩容int[] newNums = new int[nums.length*2];//数组的拷贝System.arraycopy(nums, 0, newNums, 0, nums.length);nums = newNums ;/*for(int i=0;i<nums.length;i++){newNums[i] = nums[i];}nums = newNums;*/}}/*** 向栈中存储数据* @param num* @return*/public int push(int num){//先检查容量checkCapacity();nums[index++] = num;return num ;}/*** 查看栈顶元素* @return*/public int peek(){if(index == 0) {throw new RuntimeException("栈是空的,名义数据");}return nums[index-1];}/*** 判断栈是否是空的* @return*/public boolean isEmpty(){return index == 0;}/*** 取出栈顶元素* @return*/public int pop(){if(index == 0) {throw new RuntimeException("栈是空的,名义数据");}return nums[--index];}/*** 返回 num 距离栈顶的位置* @param num* @return*/public int search(int num){int count = -1;for(int i = index-1 ;i>=0;i--){if(nums[i] == num){count = i ;}}if(count != -1) {return index - count;}return count ;}/*** 返回占中元素个数* @return*/public int size(){return index ;}}
2. 队列
队列也是一种数据结构,也是一个容器,队列的数据结构 先进先出
package com.woniu.day11.struct;import java.util.Queue;/*** add(Object o)* Object element();* offer(Object o)* peek()* Object poll()* Object remove()* @author Administrator*/public class MyQueue {String[] names;int size ;int first ;int last;public MyQueue(){this(2);}public MyQueue(int capacity){names = new String[capacity];}/*** 检查数组是否越界* public static void arraycopy(* src - 源数组。srcPos - 源数组中的起始位置。dest - 目标数组。destPos - 目的地数据中的起始位置。length - 要复制的数组元素的数量。)*/public void checkCapacity(){if(size == names.length){String[] newNames = new String[names.length*2];System.arraycopy(names, 0, newNames, 0, names.length);names = newNames ;}}/*** 返回队列的元素个数* @return*/public int size(){return this.size ;}/*** 添加元素向队列* @param name*/public void add(String name){checkCapacity(); //检查容量names[last++] = name;size++ ;}/*** 判断是否有数据* @return*/public boolean isEmpty(){return size == 0;}/*** 查看尾部元素* @return*/public String element(){if(size == 0){return null;}return names[first];}/*** 查看尾部元素* @return*/public String peek(){if(size == 0){return null;}return names[first];}/*** 取出尾部元素* @return*/public String poll(){if(size == 0){return null;}if(first <= size -1){String name = names[first];names[first] = null;first++ ;size--;return name;}else {return null;}}/*** 删除尾部元素* @return*/public String remove(){if(size == 0){return null;}if(first <= size-1) {String name = names[first];names[first] = null;first++ ;return name ;}else {return null;}}}
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.退出 ");
}
}
