3

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?

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
bap
  • 75
  • 1
  • 1
  • 5

4 Answers4

6

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]
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • 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) – Carlitos Way May 11 '17 at 21:25
  • @CarlitosWay so for that you makes a down-vote? you can do many things with `System.arraycopy` check my example – Youcef LAIDANI May 11 '17 at 21:27
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;
}
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Carlitos Way
  • 3,279
  • 20
  • 30
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.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
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;
}
IceArdor
  • 1,961
  • 19
  • 20