1

I have this method that doubles my initial array and then returns it as the new array. When I then reprint the new array, it doesn't show that it doubled. I will paste just the single method, but if it's not enough, let me know. It will print it correctly in the method when I use the second for loop, but when I call the method to print the array, it prints the initial array.

public static int [] doubleArray(int [] p)
{
    int [] newArr = new int [p.length * 2];

    for (int i = 0; i < p.length; i++)
    {
        newArr[i] = p[i];
    }

    p = newArr; //Here I set the new doubled array to equal the array in parameters

    for (int i = 0; i < p.length; i++)
    {
        System.out.print(p[i] + " "); 
    }


    return p;

}
5
  • how do you use your function? Commented Nov 20, 2013 at 18:25
  • 1
    The code "p = newArr;" will not actually change the value of p outside of the method. Try to avoid that, your code will be less confusing. Commented Nov 20, 2013 at 18:31
  • benjamin makes a good point. In most cases you would not want to set p = newArr but rather just use the newArr array to avoid reader confusion. Commented Nov 20, 2013 at 18:33
  • Ok, that answers the question. How can I make it write that to the p array and return it as the new array? Commented Nov 20, 2013 at 18:34
  • Do not modify p, just return a new array. Commented Nov 20, 2013 at 18:40

9 Answers 9

2

you're populating newArr only through length of p

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, but I am returning p. I want the rest of the array elements to be 0, which they are by default.
@user2875661 you're only printing the first p.length of them though.
@user2875661 Are you sure you're doing p = doubleArray(p); and not just doubleArray(p); ?
Ahhh! ZouZou that was the answer I was looking for! Thank you! @ZouZou
1

When you set p = newArr, the array p is still going to keep its size, so its length will not double. You could try to return newArr instead of p, which should give you the doubled array you're looking for.

Comments

0

You are only iterating to p.length. You need to modify your logic to double the length in your first for loop while retaining the correct index to use.

3 Comments

But he sets p = newArr;
@PaulDraper Sorry, meant for the first for loop.
Yes, I want the remaining spaces to stay at 0. However, I set p = newArr, so I don't know why it doesn't work the way I want it to.
0

It does doubles the length. Did you want the values to be copied too?

public static int [] doubleArray(int [] p)
{
    int [] newArr = new int [p.length * 2];

    for (int i = 0; i < p.length; i++)
    {
        newArr[i] = p[i];
        newArr[2*i] = p[i]; //copy the value
    }

    p = newArr; //Here I set the new doubled array to equal the array in parameters

    for (int i = 0; i < p.length; i++)
    {
        System.out.print(p[i] + " "); 
    }


    return p;

}

1 Comment

I wanted the values to be copied and then the remaining values to be 0.
0

length extended. try this...

public static void main(String[] args) {

    int[] a = {1,2,4};
    int[] s = doubleArray(a);
    System.out.println(Arrays.toString(s));
}

it gives output [1, 2, 4, 0, 0, 0]

Comments

0

Arrays are objects and passed by reference. So if you expect that variable you passed to this method will change it's reference you are wrong.

I ran this code and its produces array of double length and at the beginning there are values of original array. As you use array of primitive type, empty places are populated with default values(0 for int)

Comments

0

Works fine for me. Are you sure you have the right reference for the returned value?

In any case have a look at System.arraycopy

public static void main (final String args[])
{

    int [] p = {1,2,3,4,5,6,7,8,9};
    final int [] newArr = new int [p.length * 2];

    for (int i = 0; i < p.length; i++)
    {
        newArr[i] = p[i];
    }

    p = newArr; //Here I set the new doubled array to equal the array in parameters

    for (int i = 0; i < p.length; i++)
    {
        System.out.print(p[i] + " "); 
    }

}

Comments

0

Ok, @ZouZou solved the issue. When I called the method, I needed to set the initial array to equal the method. Thus when I returned the p array, it wrote it to the initial array. ie;

initialArray = doubleArray(initialArray);

Comments

0

This works totally fine for me. I guess you have called the method in main like following:

public static void main (final String args[])
{
    int [] p = {1,2,3,4,5,6,7,8,9};
    doubleArray(p);
    for (int i = 0; i < p.length; i++)
    {
        System.out.print(p[i] + " "); 
    }

}

In the doubleArray method, you didn't extend the original array. But instead, you return a new one with extended size. So the changes will not reflect to the original array outside of the method.

To get the new one, you should catch the returned array and do printing with the new one.

Is this your actual question? Please clarify!

If yes: normally you can change the original array and make the changes reflects outside the method by changing the return type to void. Those changes could be changing elements' value. But you can't change an array's size once it is declared.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.