4

Writing method to insert a[] into numbers[] at a position stored in variable "location".

    public boolean insertArray(int location, double a[])
    {    
        if (length != MAX_CAPACITY)
        {
            numbers[location] = a[];
            length++;
            return true;
        }
        return false;
    }

Is it possible to pass through an array?

3
  • 1
    What's the type of numbers? Commented May 11, 2017 at 21:00
  • Use a loop. You can use arraycopy but I guess this is exercise purpose only. Commented May 11, 2017 at 21:01
  • 1
    How can there be so many answers when we don't know the parameters of the question? Commented May 11, 2017 at 22:48

4 Answers 4

7

You can use System.arraycopy :

public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

Here is a simple example you can follow to solve your problem :

double a[] = {1, 2, 3, 4, 5};
double b[] = {6, 7, 8};
int local = 5;
double result[] = new double[a.length + b.length];

System.arraycopy(a, 0, result, 0, a.length);
System.arraycopy(b, 0, result, local, b.length);
System.out.println(Arrays.toString(result));

Output

[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
Sign up to request clarification or add additional context in comments.

2 Comments

This only copies the content of one array into another (merge)... I believe that the question asks how to insert an array (i.e. the reference to the array) into a specific location of another array (i.e. two dimensions array)
@CarlitosWay so for that you makes a down-vote? you can do many things with System.arraycopy check my example
4

Yes, you can.

But the Array has to be a Two dimensions Array! Example:

public static double[][] numbers = new double[MAX_CAPACITY][];

public boolean insertArray(int location, double[] a)
{    
    if (length != MAX_CAPACITY)
    {
        numbers[location] = a;
        length++;
        return true;
    }
    return false;
}

Comments

3

You can utilise Arrays too.

int[] numbers = ...
int[] a = ...

int n = numbers.length;
numbers = Arrays.copyOf(numbers, numbers.length + a.length);
System.arraycopy(a, 0, numbers, n, a.length);

In general List and ArrayList are better abstractions with almost the same efficiency.

Comments

2

Is there a specific reason you're using arrays instead of a List, such as an ArrayList?

If you're using a java.util.List, then use List.addAll(int location, Collection a).

If you're using arrays, then you'll need to perform the array allocation and copying yourself. Here's an example implementation of ArrayList.addAll(int, Collection) from OpenJDK:

// Copyright 1997-2007 Sun Microsystems, Inc.
public boolean addAll(int index, Collection<? extends E> c) {
    rangeCheckForAdd(index);

    Object[] a = c.toArray();
    int numNew = a.length;
    ensureCapacity(size + numNew);  // Increments modCount

    int numMoved = size - index;
    if (numMoved > 0)
        System.arraycopy(elementData, index, elementData, index + numNew,
                         numMoved);

    System.arraycopy(a, 0, elementData, index, numNew);
    size += numNew;
    return numNew != 0;
}

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.