demo
MyArrayList
package demo;
import java.util.Arrays;
public class MyArrayList {
private int[] array;
private int usedSize;
public static final int DEFAULT_SIZE = 10;
public MyArrayList(){
array = new int[DEFAULT_SIZE];
}
public void display() {
for (int i = 0; i < usedSize; i++) {
System.out.print(this.array[i] + " ");
}
System.out.println();
}
public void add(int data) {
if(this.isFull()){
array = Arrays.copyOf(array,2*array.length);
}
this.array[usedSize] = data;
this.usedSize++;
}
public boolean isFull(){
if(this.usedSize >= this.array.length){
return true;
}else{
return false;
}
}
public void add(int pos, int data) {
if(this.isFull()){
array = Arrays.copyOf(array,2*array.length);
}
if(pos < 0 || pos > this.usedSize){
System.out.println("pos位置不合理");
throw new PosWrongfulException("pos位置不合理");
}
for (int i = this.usedSize; i > pos; i--) {
this.array[i] = this.array[i-1];
}
this.array[pos] = data;
this.usedSize++;
}
public boolean contains(int toFind) {
for (int i = 0; i < this.usedSize; i++) {
if(array[i] == toFind){
return true;
}
}
return false;
}
public int indexOf(int toFind) {
for (int i = 0; i < this.usedSize; i++) {
if(this.array[i] == toFind){
return i;
}
}
return -1;
}
public int get(int pos) {
if(this.isEmpty()){
System.out.println("顺序表为空");
throw new EmptyException("当前顺序表为空!");
}
if(pos < 0 || pos > this.usedSize){
System.out.println("pos位置不合理");
throw new PosWrongfulException("pos位置不合理");
}
return this.array[pos];
}
public boolean isEmpty(){
if(this.usedSize == 0){
return true;
}
return false;
}
public void set(int pos, int value) {
if(this.isEmpty()){
System.out.println("顺序表为空");
throw new EmptyException("当前顺序表为空!");
}
if(pos < 0 || pos >= this.usedSize){
System.out.println("pos位置不合理");
throw new PosWrongfulException("pos位置不合理");
}
this.array[pos] = value;
}
public void remove(int toRemove) {
if(this.isEmpty()){
System.out.println("顺序表为空");
throw new EmptyException("当前顺序表为空!");
}
int ret = this.indexOf(toRemove);
if(ret == -1){
System.out.println("没有这个数字");
}else{
for (int i = ret; i < this.usedSize-1; i++) {
this.array[i] = this.array[i+1];
}
this.usedSize--;
}
}
public int size() {
return this.usedSize;
}
public void clear() {
this.usedSize = 0;
}
}
TestList
package demo;
public class TestList {
public static void main(String[] args) {
MyArrayList myArrayList = new MyArrayList();
myArrayList.add(1);
myArrayList.add(2);
myArrayList.add(3);
myArrayList.display();
try {
myArrayList.add(1,10);
}catch (PosWrongfulException e) {
e.printStackTrace();
}
myArrayList.display();
System.out.println("==========================");
System.out.println(myArrayList.contains(10));
System.out.println(myArrayList.contains(100));
System.out.println(myArrayList.indexOf(10));
System.out.println(myArrayList.indexOf(100));
System.out.println("==========================");
try {
System.out.println(myArrayList.get(1));
}catch (PosWrongfulException e) {
e.printStackTrace();
}
myArrayList.set(0,99);
myArrayList.display();
System.out.println("==========================");
myArrayList.remove(10);
myArrayList.display();
System.out.println("==========================");
myArrayList.clear();
myArrayList.display();
myArrayList.add(19999);
myArrayList.display();
}
}
EmptyException
package demo;
public class EmptyException extends RuntimeException{
public EmptyException(){
}
public EmptyException(String message){
super(message);
}
}
PosWrongfulException
package demo;
public class PosWrongfulException extends RuntimeException{
public PosWrongfulException(){
}
public PosWrongfulException(String message){
super(message);
}
}
结果
1 2 3
1 10 2 3
==========================
true
false
1
-1
==========================
10
99 10 2 3
==========================
99 2 3
==========================
19999