代码拉取完成,页面将自动刷新
package Week03;
import jsjf.EmptyCollectionException;
public class ArrayList {
private final int DEFAULT_CAPACITY = 100;
private int nWangYuHan = 0;
int[] array,temp;
public ArrayList(){
array = new int[DEFAULT_CAPACITY];
}
public void add(int number){
if (size() ==array.length) {
ExpandCapacity();
}
array[nWangYuHan] = number;
nWangYuHan++;
}
public void Insert(int number, int address){
if (address > array.length){
System.out.println("错误!");
}
else{
temp = new int[size() - address];
int n = 0;
for(int a = address; a < size(); a++){
temp[n] = array[a];
n++;
}
array[address] = number;
for(int b = 0; b < temp.length;b++){
array[address + b + 1] = temp[b];
}
}
nWangYuHan++;
}
public void Delete(int pos) throws EmptyCollectionException {
int c = pos;
if(isEmpty()) {
throw new EmptyCollectionException("StackADT");
}
temp = new int[nWangYuHan - pos];
for(int n = 0; n < temp.length; n++){
temp[n] = array[pos + 1];
pos++;
}
for(int s = 0; s < temp.length; s++){
array[c] = temp[s];
c++;
}
nWangYuHan--;
}
public void Sort() {
String result = "";
for (int x = 0; x < size(); x++){
result += array[x]+ " ";
}
System.out.println("排序前的数组为:" + result + "当前有" + nWangYuHan + "个元素在数组中");
int n = 0;
for (int i = 0; i < array.length - 1; i++){
for (int j = i + 1;j < size();j++){
if (array[i] > array[j]){
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
n++;
int wangyuhan = size();
String str = "";
for (int x = 0; x < size(); x++){
str += array[x]+ " ";
}
System.out.println("(第"+ n +"次排序)");
System.out.println("当前数组为:" + str + "有" + wangyuhan + "个元素在数组中");
}
}
}
private boolean isEmpty() {
return nWangYuHan == 0;
}
private void ExpandCapacity() {
int[] larger = new int[array.length + 1];
for (int x = 0; x < array.length; x++){
larger[x] = array[x];
}
array = larger;
}
protected int size(){
return nWangYuHan;
}
public String toString(){
String result = "";
for (int count = 0; count < size();count++){
result += array[count] + " ";
}
return result;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。