ArrayList类的实现

package other;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

/*
 * ArrayList泛型类的实现
 */
public class MyArrayList<AnyType> implements Iterable<AnyType> {

	private static final int DEFAULT_CAPACITY = 10;

	private int theSize;
	private AnyType[] theItems;

	public MyArrayList() {
		clear();
	}

	public void clear() {
		theSize = 0;
		ensureCapacity(DEFAULT_CAPACITY);
	}

	private int size() {
		return theSize;
	}

	public boolean isEmpty() {
		return size() == 0;
	}

	public void trimToSize() {
		ensureCapacity(size());
	}

	public AnyType get(int index) {
		if (index < 0 || index >= size()) {
			throw new ArrayIndexOutOfBoundsException();
		}
		return theItems[index];
	}

	private AnyType set(int index, AnyType newVal) {
		if (index < 0 || index >= size()) {
			throw new ArrayIndexOutOfBoundsException();
		}
		AnyType old = theItems[index];
		theItems[index] = newVal;
		return old;
	}

	public void ensureCapacity(int newCapacity) {
		if (newCapacity < theSize) {
			return;
		}

		AnyType[] old = theItems;
		theItems = (AnyType[]) new Object[newCapacity];
		for (int i = 0; i < size(); i++) {
			theItems[i] = old[i];
		}
	}

	public void add(int index, AnyType x) {
		if (theItems.length == size()) {
			ensureCapacity(size() * 2 + 1);
		}
		for (int i = theSize; i < index; i--) {
			theItems[i] = theItems[i - 1];
		}
		theItems[index] = x;

		theSize++;
	}

	public boolean add(AnyType x) {
		add(size(), x);
		return true;
	}

	public AnyType remove(int index) {
		AnyType removedItem = theItems[index];
		for (int i = index; i < size() - 1; i++) {
			theItems[i] = theItems[i + 1];
		}
		theSize--;
		return removedItem;
	}

	public Iterator<AnyType> iterator() {

		return new ArrayListIterator();
	}

	private class ArrayListIterator implements Iterator<AnyType> {

		private int current = 0;

		public boolean hasNext() {

			return current < size();
		}

		public AnyType next() {

			if (!hasNext()) {
				throw new NoSuchElementException();
			}
			return theItems[current++];
		}

		public void remove() {
			MyArrayList.this.remove(--current);
		}

	}

}

 

转载于:https://www.cnblogs.com/yoyohong/p/5651808.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解答这个问题。 首先,我们需要定义一个银行账户,包含账户名、账户余额等属性,以及存款、取款等方法。然后,使用ArrayList来存储多个银行账户对象。 下面是一个简单的示例代码: ```java import java.util.ArrayList; public class BankAccount { private String accountName; private double balance; public BankAccount(String accountName, double balance) { this.accountName = accountName; this.balance = balance; } public void deposit(double amount) { balance += amount; } public void withdraw(double amount) { if (balance >= amount) { balance -= amount; } else { System.out.println("余额不足!"); } } public String getAccountName() { return accountName; } public double getBalance() { return balance; } public static void main(String[] args) { ArrayList<BankAccount> accounts = new ArrayList<>(); accounts.add(new BankAccount("张三", 1000)); accounts.add(new BankAccount("李四", 2000)); for (BankAccount account : accounts) { System.out.println(account.getAccountName() + "的账户余额为:" + account.getBalance()); account.deposit(500); System.out.println(account.getAccountName() + "存入500元后,账户余额为:" + account.getBalance()); account.withdraw(1000); System.out.println(account.getAccountName() + "取出1000元后,账户余额为:" + account.getBalance()); } } } ``` 在这个示例中,我们定义了一个BankAccount,包含了账户名、账户余额等属性,以及存款、取款等方法。在main方法中,我们使用ArrayList来存储多个银行账户对象,并分别对每个账户进行存款、取款操作。 在实际开发中,我们还可以添加更多的功能,例如查询账户列表、根据账户名查找账户等。同时,我们还需要考虑多线程并发访问的安全性问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值