1 Star 0 Fork 0

20172323王禹涵/20172323王禹涵new

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ArrayList.java 2.87 KB
一键复制 编辑 原始数据 按行查看 历史
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;
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/Lewandodoski/20172323_wang_yuhan_new.git
git@gitee.com:Lewandodoski/20172323_wang_yuhan_new.git
Lewandodoski
20172323_wang_yuhan_new
20172323王禹涵new
master

搜索帮助

Cb406eda 1850385 E526c682 1850385