3

I have two Arrays that are initialized as such:

    public static double[] arrayweight= new double[100];
    public static double[] arraypulse= new double[100];

They are filled with data e.g. 23.0,25.8....etc.

I need to combine the two Arrays into one array of double[]

I have chosen to use the following approach which does not work. Any ideas?

     ArrayList <Double> al = new ArrayList<Double>();

// The following methods do not work ;(     
     al.add((double[]) Global.arrayweight);
     al.add(new Double(Global.arraypulse));
aalku
  • 2,795
  • 1
  • 21
  • 44
Trey Balut
  • 1,301
  • 3
  • 15
  • 37
  • Strongly related: http://stackoverflow.com/questions/80476/how-to-concatenate-two-arrays-in-java – Ray Toal Sep 11 '11 at 04:00
  • `Arrayweight` and `Arraypulse` should be named `arrayWeight` `arrayPulse`. Look up Java naming conventions. – Miserable Variable Sep 11 '11 at 05:11
  • The types (and cardinalities) do not match at all. You are trying to add a whole array as an element of the list that is not a list of arrays, and then trying to convert an array to a single Double object. – aalku Sep 11 '11 at 19:05

6 Answers6

3

You can achieve it using System.arraycopy.

double[] cArray= new double[Arrayweight.length + Arraypulse.length];
System.arraycopy(Arrayweight, 0, cArray, 0, Arrayweight.length);
System.arraycopy(Arraypulse, 0, cArray, Arrayweight.length, Arraypulse.length);
Mahesh
  • 33,625
  • 17
  • 84
  • 113
2

How about the easy way:

double[] arr = new double[Arrayweight.length + Arraypulse.length];
int counter = 0;
for(double d1 : Arrayweight) arr[counter++] = d1;
for(double d2 : Arraypulse)  arr[counter++] = d2;

or (if they have same length):

int length = Arrayweight.length + Arraypulse.length;
double[] arr = new double[length];
for(int i = 0; i < length / 2; i++)
{
    arr[i] = Arrayweight[i];
    arr[i + length / 2] = Arraypulse[i];
}
Eng.Fouad
  • 111,301
  • 67
  • 311
  • 403
1

You might find TDoubleArrayList useful. This is a wrapper for double[].

TDoubleArrayList al = new TDoubleArrayList();
al.add(Arrayweight);
al.add(Arraypulse);

However your naming suggest you are using arrays when objects might be a better approach.

class MyData {
    double weight, pulse;
}

List<MyData> al = new ArrayList<MyData>();
for(int i=0;i<100;i++) {
    MyData md = new MyData();
    md.weight = ...
    md.pulse = ...
    al.add(md);
}
Peter Lawrey
  • 513,304
  • 74
  • 731
  • 1,106
0
double[] both = (double[]) ArrayUtils.addAll(Arrayweight, Arraypulse);
confucius
  • 12,867
  • 10
  • 46
  • 66
0

I like Nammari's answer best, but just in case you are not using Commons Lang and want to stick with pure Java:

double[] result = Arrays.copyOf(Arrayweight, Arrayweight.length + Arraypulse.length);
System.arrayCopy(Arraypulse, 0, result, Arrayweight.length, Arraypulse.length);
Ray Toal
  • 82,964
  • 16
  • 166
  • 221
-1

The add method takes individual values, not arrays. You can combine List's addAll and Arrays' asList method (if you want to stick with an ArrayList):

al.addAll(Arrays.asList(Global.Arrayweight));
al.addAll(Arrays.asList(Global.Arraypulse));
Klarth
  • 2,015
  • 14
  • 26