user695696
user695696

Reputation: 155

How do I add a parameter to the end of an array?

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

Answers (6)

Adi Mor
Adi Mor

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

MikeU
MikeU

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

mikera
mikera

Reputation: 106351

I'd suggest a few changes to the implementation of your NumberList class:

  • Add a "count" instance variable to your NumberList class, which represents the number of integers in the list. This will be useful in the case that you want to allocate a longer array than you currently have integers, e.g. you can have a length 10 array initially but have count==0
  • Then if you want to add a new integer and still have space in the array, all you need to do is put the integer in the right place in the array and do count++
  • When you need to reallocate the array (i.e. if count == array.length when you try to add a new integer), then you should make the new array quite a bit larger, e.g. double the size of the previous array. This will avoid reallocating the array with every single integer added.
  • Use System.arraycopy to copy the old array values into the new array (with a length equal to "count")

Upvotes: 0

Dastyruck
Dastyruck

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

Greg Hewgill
Greg Hewgill

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

nevets1219
nevets1219

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

Related Questions