数组队列总结

1.数组队列的原理:

数组的物理内存大小不可改变,要使得可以在数组中添加或者删除元素,就只能创建一个新的数组,把原来数组的的值赋给新数组,再把要添加的元素放到新数组末尾,由于数组名存放的是内存首地址,只要把新数组的首地址赋给原数组名就可以啦

2.数组队列的实现:

实现队列的思路:在实现类的内部,使用数组保存装入队列的对象,每次加入新对象时,则创建一个比原来数组长度大1的数组

 

代码:

public class Student{

private String name;

private int score;

public Student(String name,int score){

this.name=name;

thhis.score=score;

}

public void setName(String name){

this.name=name;

}

public String getName(){

return name;

}

public void setScore(int score){

this. score = score ;

}

public int getScore (){

return  score ;

}

public void showInfo(){

System.out.println("姓名:"+name+"学分:"+score);

}

}

 

 

public interface ArrayList<E>{

public void add(E e);

public E get(int index);

public int size();

}

 

 

public class BooList<E>  implements ArrayList<E>{

private object[] srA=new Object[0];

public void add(E e){

Object[] newArray=new Object[srA.length+1];

newArray[srA.length]=e;

for(int i=0;i<srA.length;i++){

newArray[i]=srA[i];

}

srA=newArray;

}

public E get(int index){

E st=(E)srA[index];

return st;

}

public int size(){

return srA.length;

}

}

 

 

 

public class MainList{

public static void main(String args[]){

ArrayList<Student> al=new BooList();

for(int i=0;i<5;i++){

Student st=new Student("name"+i;i+10);

al.add(st);

}

System.out.println(a1.size()+"个学生信息如下");

for(int t=0;t<al.get(t);t++){

Student st=al.get(t);

st.showInfo();

}

}

}

你可能感兴趣的:(数组队列)