📄 arraylist.java
字号:
/*
*提供具体的业务逻辑的实现,业务逻辑层
*/
package com.work05;
public class ArrayList
{
private Student array[];
private int size; //有效元素当前的坐标
private int maxSize; //最多存储的个数
public ArrayList(int size)
{
array=new Student[size];
maxSize=size;
}
public ArrayList()
{
array=new Student[20];
maxSize=20;
}
private void testArray()//因为对外界无用
{
if(size>=maxSize)
{
Student newArray[]=new Student[maxSize+20];
System.arraycopy(array,0,newArray,0,size);
array=newArray;
maxSize+=20;
}
}
public boolean add(Student o)//有序添加一个Student类型的对象以数组表示
{
if(size==0)
{
array[size]=o;
size++;
return true;
}
int index=-1;
//查找应该插入的位置
for(int i=0;i<size;i++)
{
//判断是否有重复id
if(array[i].equals(o))
return false;
if(o.getId()<array[i].getId())
{
index=i;
break;
}
}
testArray();
//最后位置的插入
if(index==-1)
{
array[size]=o;
size++;
return true;
}
//中间位置的插入
for(int i=size;i>index;i--)
array[i]=array[i-1];
array[index]=o;
size++;
return true;
}
public Student find(int id)//二分法查找
{
int lowerBound=0;
int upperBound=size-1;
int currentValue;
while(true)
{
currentValue=(lowerBound+upperBound)/2;
if(array[currentValue].getId()==id)
return array[currentValue];
else if(lowerBound>upperBound)
return null;
else//不确认没有找到,允许改变边界再次找到
if(array[currentValue].getId()<id)
lowerBound=currentValue+1;
else
upperBound=currentValue-1;
}
}
public void printStruct()
{
System.out.println("ID Name CENT");
}
public void print()//输出所有学员的信息
{
printStruct();
for(int i=0;i<size;i++)
{
array[i].print();
}
}
public void print(int id)
{
printStruct();
Student stu=find(id);
if(stu!=null)
{
printStruct();
stu.print();
}
else
System.out.println("the Id is not exist");
}
public void bubblePrint()
{
Student newArray[]=new Student[size];
System.arraycopy(array,0,newArray,0,size);
for(int j=size-1;j>=1;j++)
{
for(int i=0;i<j;i++)
{
if(array[i].getCent()>array[i+1].getCent())
{
Student temp=newArray[i];
array[i]=array[i+1];
array[i+1]=temp;
}
}
}
printStruct();
for(int i=0;i<size;i++)
newArray[i].print();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -