Insert array into another array


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?


You can use <a href="https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#arraycopy(java.lang.Object,%20int,%20java.lang.Object,%20int,%20int)" rel="nofollow">System.arraycopy</a> :

public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) <hr />

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));


[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]


<strong>Yes, you can.</strong>

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; }


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.



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 <a href="https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#addAll%28int,%20java.util.Collection%29" rel="nofollow">List.addAll(int location, Collection a)</a>.

If you're using arrays, then you'll need to perform the array allocation and copying yourself. Here's an <a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/ArrayList.java#ArrayList.addAll%28int%2Cjava.util.Collection%29" rel="nofollow">example implementation</a> 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; }


  • why autosuggest not display and how to add filter functionalty
  • Cover the methods in inheritance
  • Unable to replicate an encryption format from Java to PHP
  • Why direct memory 'array' is slower to clear than a usual Java array?
  • Add vcard to Contacts with Mail rules and Applescript
  • Copy folder content with Ansible
  • Thrust filter by key value
  • C++ Single function pointer for all template instances
  • Can I have a variable number of URI parameters or key-value pairs in Laravel 4?
  • How to view images from protected folder with php?
  • Unable to install Git-core+svn by MacPorts
  • Django simple Captcha “No module named fields” error
  • Display images in Django
  • Test if a set exists before trying to drop it
  • Could not find rake using whenever rails
  • Django: Count of Group Elements
  • Q promise. Difference between .when and .then
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Resize panoramic image to fixed size
  • Incrementing object id automatically JS constructor (static method and variable)
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Modifying destination and filename of gulp-svg-sprite
  • sending/ receiving email in Java
  • When should I choose bucket sort over other sorting algorithms?
  • Importing jscolor library in angular 2
  • Hazelcast - OperationTimeoutException
  • To display the title for the current loaction in map in iphone
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • How to delete a row from a dynamic generate table using jquery?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Proper way to use connect-multiparty with express.js?
  • How do I configure my settings file to work with unit tests?
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Is it possible to post an object from jquery to bottle.py?
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?