Java – 数组越界问题?

huangapple go评论87阅读模式
英文:

Java - arraylist out of bounds?

问题

我不知道这里出了什么问题。据我理解,它应该是工作的:

public void deposit(int accNum, double balance)
{
    for (int i = 0; i < numAccounts; i++)
    {
        if (accNum == accounts[i].getAccNum())
        {
            accounts[i].deposit(balance);
            Transaction temp = new Transaction(accNum, 1, balance, CurrentDateTime());
            if (this.transactions.size() <= 100)
            {
                this.transactions.set(1, temp);
            }
            else
            {
                this.transactions.remove(100);
                this.transactions.set(1, temp);     //移除最旧的
                System.out.println("请注意,您最旧的交易已被覆盖。");
            }
        }
        else
        {
            System.out.println("没有找到此账户记录。");
        }
    }
}

我得到了以下错误:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
    at java.base/java.util.Objects.checkIndex(Objects.java:373)
    at java.base/java.util.ArrayList.set(ArrayList.java:441)
    at Bank.deposit(Bank.java:145)         //引用于此代码段,特别是在 this.transactions.set(i, temp);
    at BankDemo1.main(BankDemo1.java:11)

Process finished with exit code 1

我尝试过更改 transactions 的大小为其他数字,但始终会得到 "0 超出了大小为 0 的界限"。

transactions 被定义为:

private ArrayList<Transaction> transactions = new ArrayList<Transaction>(100);

这将是放入 transactions 的第一个元素(在调用该方法从演示中调用时,transactions 中之前没有任何内容)。

演示中的相关代码部分为:

testBank.deposit(1000, 150.25);

其中第一个数字是要存入的帐户号码,第二个数字是要存入的金额。

英文:

I don't know what's going wrong here. As I understand it, it should be working:

public void deposit(int accNum, double balance)
    {
        for (int i = 0; i &lt; numAccounts; i++)
        {
            if (accNum == accounts[i].getAccNum())
            {
                accounts[i].deposit(balance);
                Transaction temp = new Transaction(accNum, 1, balance, CurrentDateTime());
                if (this.transactions.size() &lt;= 100)
                {
                    this.transactions.set(1, temp);
                }
                else
                {
                    this.transactions.remove(100);
                    this.transactions.set(1, temp);     //remove oldest
                    System.out.println(&quot;Please note that your oldest transaction has been overwritten.&quot;);
                }
            }
            else
            {
                System.out.println(&quot;No such account on record.&quot;);
            }
        }
    }

I am given the following error:

Exception in thread &quot;main&quot; java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
	at java.base/java.util.Objects.checkIndex(Objects.java:373)
	at java.base/java.util.ArrayList.set(ArrayList.java:441)
	at Bank.deposit(Bank.java:145)         //in reference to this snippet, specifically at this.transactions.set(i, temp);
	at BankDemo1.main(BankDemo1.java:11)

Process finished with exit code 1

I've tried changing the size of transactions to be other numbers but I always get 0 is out of bounds for size 0.

transactions is defined as

private ArrayList&lt;Transaction&gt; transactions = new ArrayList&lt;Transaction&gt;(100);

This would be the first element put into transactions (as in, there is nothing in transactions before this when the method is called from the demo)

The relevant portion of code from Demo is

        testBank.deposit(1000, 150.25);

where the first number is an account number to be deposited into, and the second the amount to be deposited.

答案1

得分: 2

当您执行new ArrayList&lt;&gt;(100)时,并不实际创建一个具有100个元素的列表,100只是底层数组的初始容量。

您可能希望使用add()方法并带有索引作为参数,以确保您的列表实际上具有那么多元素。

英文:

When you do new ArrayList&lt;&gt;(100), you don't actually create a List with 100 elements, 100 is just the initial capacity of the underlying array.

You might want to use add() method with index as an argument to ensure that your list actually has that much elements.

huangapple
  • 本文由 发表于 2020年10月8日 07:09:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64253541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定