How can I reinitialize an array in a method and have the new size update in main?

My teacher gave our AP Computer Science class one of the free response questions from our upcoming test. The question says:

Write a method public static void insert(int[] a, int n, int x) that inserts x in order among the first n elements of a, assuming these elements are arranged in ascending order.

So practically, she wants us to send of an array of x values and get an array back with x + 1 values. Is that even possible while using a void function? Thanks in advance.

/**
 * Console output:
 * 
 * Array 'a' in insert method after inserting x: [4, 6, 7, 8]
 * Array 'a' in main method after insert has been called: [4, 6, 7]
 */

package chapter14Test;

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;

public class Chapter14Test
{
    public static void insert(int[] a, int n, int x)
    {
        //declare variables
        List<Integer> aList = new ArrayList<Integer>();
        Integer[] tempA = new Integer[a.length + 1];

        //traverse a and add values to aList
        for (int i = 0; i < a.length; i++)
        {
            //add a value to aList
            aList.add(a[i]);
        } //end for

        //traverse aList until n + 1 is reached
        for (int i = 0; i < n; i++)
        {
            //determine if aList value is greater than x
            if (aList.get(i).intValue() >= x)
            {
                //add x at index i
                aList.add(i, x);

                //exit loop
                i = n + 1;
            } //end if
        } //end if

        //determine if x needs to be added at the end
        if (x > aList.get(aList.size() - 1).intValue())
        {
            //add x to end of aList
            aList.add(x);
        } //end if

        //convert aList to an array
        tempA = aList.toArray(new Integer[0]);

        //reinitialize a
        a = new int[tempA.length];

        //transfer tempA values to a
        for (int i = 0; i < tempA.length; i++)
        {
            //transfer value at tempA element to a element
            a[i] = tempA[i].intValue();
        } //end for

        //display a
        System.out.println("Array 'a' in insert method after inserting x: " + Arrays.toString(a));
    } //insert

    public static void main(String[] args)
    {
        //declare array
        int[] a = new int[] {4, 6, 7};

        //insert 8 among the first two elements in a
        insert(a, 2, 5);

        //display a
        System.out.println("Array 'a' in main method after insert has been called: " + Arrays.toString(a));
    } //main
}

Answers


It is possible. So when you write the below statement you pass the address or reference of a and hence whatever changes your insert method will make will eventually be reflected back in the array a of your main method.

insert(a, 2, 5);

Insert method will modify a. You don't have to return it back. The changes will be reflected in your main method.

Note: It won't happen if a has to be enlarged to store the new values.


Need Your Help

- (void)applicationWillTerminate:(UIApplication *)application

objective-c iphone cocoa-touch

I found this method in a delegate file and inserted some code. The code never gets executed, even when the home button is pressed. When or how does this function get called?

Generate resultset according to report

mysql sql count reporting aggregate-functions

How do I generate the following reports using MYSQL?