第一次手写List

	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();
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值