Java Array Merge merge(Object[][] array1, Object[][] array2)

Here you can find the source of merge(Object[][] array1, Object[][] array2)

Description

merge

License

Open Source License

Declaration

public static Object[][] merge(Object[][] array1, Object[][] array2) 

Method Source Code

//package com.java2s;
/*//from w w w . jav  a 2 s.  c  o  m
 * 
This file is part of YaBS.
    
YaBS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
YaBS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
    
 * 
 */

import java.util.ArrayList;

import java.util.Iterator;

public class Main {
    public static Object[][] merge(Object[][] array1, Object[][] array2) {
        if (array1 == null) {
            array1 = new Object[0][0];
        }
        if (array2 == null) {
            array2 = new Object[0][0];
        }

        int z = 0;
        if (array1 != null && array1.length > 0) {
            z = array1[0].length;
        }

        if (array2 != null && array2.length > 0) {
            if (z < array2[0].length) {
                z = array2[0].length;
            }
        }

        Object[][] mergedArray = new Object[array1.length + array2.length][z];
        int i = 0;

        for (i = 0; i < array1.length; i++) {
            for (int k = 0; k < array1[i].length; k++) {
                mergedArray[i][k] = array1[i][k];
            }
        }

        for (int l = 0; l < array2.length; l++) {
            for (int k = 0; k < array2[l].length; k++) {
                mergedArray[i + l][k] = array2[l][k];
            }
        }
        return mergedArray;
    }

    public static Object[][] merge(Object[] array1, Object[][] array2) {
        if (array1 == null) {
            array1 = new Object[0];
        }
        if (array2 == null) {
            array2 = new Object[0][0];
        }

        int z = 0;
        if (array1 != null && array1.length > 0) {
            z = array1.length;
        } else if (array2 != null && array2.length > 0) {
            z = array2[0].length;
        } else {
            z = 0;
        }

        Object[][] mergedArray = new Object[array1.length + array2.length][z];
        int i = 0;
        for (i = 0; i < array1.length; i++) {
            mergedArray[0][i] = array1[i];
        }

        for (int l = 0; l < array2.length; l++) {
            for (int k = 0; k < array2[l].length; k++) {
                mergedArray[i + l][k] = array2[l][k];
            }
        }
        return mergedArray;
    }

    public static Object[] merge(Object[] array1, Object[] array2) {
        if (array1 == null) {
            array1 = new Object[0];
        }
        if (array2 == null) {
            array2 = new Object[0];
        }

        Object[] mergedArray = new Object[array1.length + array2.length];
        int i = 0;

        for (i = 0; i < array1.length; i++) {
            mergedArray[i] = array1[i];
        }

        for (int l = 0; l < array2.length; l++) {
            mergedArray[i + l] = array2[l];
        }

        return mergedArray;
    }

    @SuppressWarnings("unchecked")
    public static ArrayList merge(ArrayList list1, ArrayList list2) {
        Iterator it1 = list1.iterator();
        Iterator it2 = list2.iterator();

        ArrayList<Double> list3 = new ArrayList();

        while (it1.hasNext()) {
            list3.add((Double) it1.next());
        }

        while (it2.hasNext()) {
            list3.add((Double) it2.next());
        }

        return list3;
    }

    /**
     * list 1 + 2 must have same element count!
     *
     * @param list1
     * @param list2
     * @return list1.values(n) + list2.values(n)
     * @throws Exception
     */
    public static ArrayList<Double> add(ArrayList<Double> list1, ArrayList<Double> list2) throws Exception {

        Iterator it1 = list1.iterator();
        Iterator it2 = list2.iterator();

        if (list1.size() != list2.size()) {
            throw new Exception("list 1 + 2 must have same element count!");
        }
        @SuppressWarnings("unchecked")
        ArrayList<Double> list3 = new ArrayList();

        while (it1.hasNext() && it2.hasNext()) {
            Double value = (Double) it1.next() + (Double) it2.next();
            list3.add(value);
        }

        return list3;
    }
}

Related

  1. merge(int[] array, int i, int mid, int max)
  2. merge(int[] array1, int[] array2)
  3. merge(int[] x1, int[] x2)
  4. merge(int[]... arrs)
  5. merge(long[] theArray, long[] workSpace, int lowPtr, int highPtr, int upperBound)
  6. merge(String array[], String delimiter)
  7. merge(String[] array)
  8. merge(String[] array1, String[] array2)
  9. merge(String[] strArr1, String[] strArr2)