import java.util.Arrays;
public class Test1 {
public static void main(String[] args) {
MyList A = new MyList();
A.add(11);
System.out.println(A.toString());
A.add(12);
A.add(13);
A.add(14);
A.add(15);
A.add(16);
A.add(17);
A.add(18);
A.add(19);
A.add(20);
System.out.println(A.toString());
A.add(21);
A.remove(9);
System.out.println(A.toString());
System.out.println(A.get(1));
System.out.println(A.size());
A.remove(40);
}
}
package homework716;
import java.util.Arrays;
import java.util.Scanner;
public class MyList {
Integer[] myList = new Integer[10];
int count = 0;
public void add(int value) {
if(count == myList.length) {
myList = Arrays.copyOf(myList, myList.length*2);
}
myList[count] = value;
count++;
}
public int get(int index) {
return myList[index - 1];
}
public void remove(int index) {
if(index == 1) {
System.arraycopy(myList, index, myList, index - 1, myList.length - 1);
myList[myList.length - 1] = null;
}
if(index > myList.length) {
System.out.println("该数据不存在!");
return;
}
if(index > 1){
System.arraycopy(myList, 0, myList, 0, index - 1);
System.arraycopy(myList, index, myList, index - 1, myList.length - index);
myList[myList.length - 1] = null;
count--;
}
}
public int size() {
return count;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for(int i = 0;i<size();i++) {
sb.append(myList[i] + ",");
}
sb.replace(sb.length()-1, sb.length(), "]");
return sb.toString();
}
}