So I am trying to add an element to the end of an array. What I've tried to do is make a new array that is bigger to accommodate the new array. And then I tried to add an element to the array. The only problem is that once an element, all the elements of the array get copied over.
accountId = Arrays.copyOf(accountId, accountId.length + 1);
money = Arrays.copyOf(money, money.length + 1);
for (int i = 0; i < accountId.length; i++) {
accountId[i] = id;
}
for (int i = 0; i < money.length; i++) {
money[i] = opBalance;
}
I Would appreciate any help!
You don't need loops after you copy the arrays. Just set the last element. Also, I would prefer a Plain Old Java Object (POJO) to two parallel arrays.
accountId = Arrays.copyOf(accountId, accountId.length + 1);
money = Arrays.copyOf(money, money.length + 1);
accountId[accountId.length - 1] = id;
money[money.length - 1] = opBalance;