Reputation: 155
I am working on this NumberList class which represents a list of integers. The NumberList object has just one instance variable, which is a reference to an array of int values. One of the methods I need to implement is suppose to add the parameter to the end of the list by:
a) creating another array that is one unit larger than the existing one
b) Copying all of the elements from the existing array over to the new one
c) Adding the parameter to the end of the new array
d) Re-assigning the instance variable "values" so that it refers to the new array.
Here is my attempt at it. There are no errors but I feel like it is incorrect especially the part where I try to add number to the end of anotherArray. The parameter I am referring to is "number", an int
public void add(int number) {
int[] anotherArray;
int newLength = values.length + 1;
anotherArray = new int[newLength];
for (int i = 0; i <values.length; i++)
values[i] = anotherArray[i];
for (int i = 0; i < anotherArray[i]; i++)
anotherArray[i] += number;
values = new int[anotherArray.length];
}
Upvotes: 1
Views: 12397
Reputation: 2165
you don't need the second for loop, you just need to add the last number at the last position of the array.
public void add(int number) {
int[] anotherArray;
int newLength = values.length + 1;
anotherArray = new int[newLength];
for (int i = 0; i <values.length; i++)
values[i] = anotherArray[i];
//for (int i = 0; i < anotherArray[i]; i++)
anotherArray[anotherArray.lenth-1] = number;
values = new int[anotherArray.length];
}
Upvotes: 0
Reputation: 11
Look at parts b, c, and d in your algorithm, and compare them carefully with your code.
The first for-loop performs part b. But compare your code with the statement "Copying all of the elements from the existing array over to the new one." Is that what your code is doing? Not quite, but almost. Your code assigns values from the new array to the existing array.
Now think about the statement in part c. Does this require a loop? No, it does not. But first, let's discuss what's wrong with this loop, as a learning exercise. First, it may generate an IndexOutOfBoundsException because your loop condition is i < anotherArray[i]
. The values in the array have nothing to do with their size, so if all the values are large, i
will increment past the end of the array. Also, why are you adding number
to each element?
Now, as for fixing it, take a look at statement c. It says to add it to the end of the new array. In this context (and in all contexts involving data structures), "add" means "insert" and not "+". You created a new array with one extra element; that element is current empty. This new element is at the end, and needs to hold number
.
Finally, your last statement just makes values
point to a new, empty array. Instead, you should make the values
reference point to the array that you already created and filled with values.
Upvotes: 0
Reputation: 106351
I'd suggest a few changes to the implementation of your NumberList class:
Upvotes: 0
Reputation: 96
It looks like for the most part, you are on the right track, but it starts to derail when you try to add to the end of the new array...
From what I can tell, all you need is this...
`public void add(int number) { int[] anotherArray; int newLength = values.length + 1;
anotherArray = new int[newLength];
for (int i = 0; i <values.length; i++)
anotherArray[i] = values[i];
anotherArray[newLength-1] = number;
values = anotherArray.length;
} `
Upvotes: 0
Reputation: 993005
First, you've got the assignment backwards. It should be this way around:
for (int i = 0; i < values.length; i++)
anotherArray[i] = values[i];
This assigns from values
to anotherArray
. Second, you want to set the new value in anotherArray
, like this:
anotherArray[newLength - 1] = number;
Finally,
values = anotherArray;
Here's another way to write that code using System.arraycopy
:
public void add(int number) {
int[] anotherArray = new int[values.length + 1];
System.arraycopy(values, 0, anotherArray, 0, values.length);
anotherArray[values.length] = number;
values = anotherArray;
}
Upvotes: 4
Reputation: 7706
You don't need two for
loops to do this because you just need to copy the elements from the old array into the new array once.
You also do not want to do values = new int[anotherArray.length];
because that is essentially declaring a new array with no values set.
The logic should look something like:
newArray = new Array[old_length + 1];
for ( i from 0 to old_length ) {
newArray[i] = values[i];
}
newArray[old_length] = new_element
values = newArray
Upvotes: 0