Java Array Merge mergeColumnsSafely(int[] arrOriginalColumns, int[] arrExtraColumns)

Here you can find the source of mergeColumnsSafely(int[] arrOriginalColumns, int[] arrExtraColumns)

Description

Merge new columns to the list of existing columns safely so if they already exist in the original list, they won't be added.

License

Open Source License

Parameter

Parameter Description
arrOriginalColumns - list of original columns
arrExtraColumns - new columns to add to the list

Return

int[] - if the list already contained the columns, it will return the same list otherwise it will return new list with extra columns

Declaration

public static int[] mergeColumnsSafely(int[] arrOriginalColumns, int[] arrExtraColumns) 

Method Source Code

//package com.java2s;
/*// w w  w  .  ja v  a  2 s.  c o  m
 * Copyright (C) 2003 - 2013 OpenSubsystems.com/net/org and its owners. All rights reserved.
 * 
 * This file is part of OpenSubsystems.
 *
 * OpenSubsystems is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Merge new columns to the list of existing columns safely so if they already
     * exist in the original list, they won't be added. 
     * 
     * @param arrOriginalColumns - list of original columns
     * @param arrExtraColumns - new columns to add to the list
     * @return int[] - if the list already contained the columns, it will return
     *                  the same list otherwise it will return new list
     *                  with extra columns
     */
    public static int[] mergeColumnsSafely(int[] arrOriginalColumns, int[] arrExtraColumns) {
        int[] arrReturn = arrOriginalColumns;
        List lstAddedColumns = new ArrayList();
        int iIndex;
        int iIndex1;
        Integer iColumn;
        boolean bFoundFlag;

        // Try to find if the columns already exist in the list
        for (iIndex = 0; iIndex < arrExtraColumns.length; iIndex++) {
            bFoundFlag = false;
            for (iIndex1 = 0; iIndex1 < arrOriginalColumns.length; iIndex1++) {
                if (arrExtraColumns[iIndex] == arrOriginalColumns[iIndex1]) {
                    // Column is already there
                    bFoundFlag = true;
                    break;
                }
            }
            if (!bFoundFlag) {
                // new column value not found
                lstAddedColumns.add(new Integer(arrExtraColumns[iIndex]));
            }
        }

        if (lstAddedColumns.size() > 0) {
            // There are not some columns yet, copy the original elements
            arrReturn = new int[arrOriginalColumns.length + lstAddedColumns.size()];
            System.arraycopy(arrOriginalColumns, 0, arrReturn, 0, arrOriginalColumns.length);

            // Add new columns to the original 
            for (iIndex = 0; iIndex < lstAddedColumns.size(); iIndex++) {
                iColumn = (Integer) lstAddedColumns.get(iIndex);
                arrReturn[arrOriginalColumns.length + iIndex] = iColumn.intValue();
            }
        }

        return arrReturn;
    }
}

Related

  1. mergeByteArraies(byte[] array1, byte[] array2)
  2. mergeByteArrays(byte[] array1, byte[] array2)
  3. mergeByteArrays(byte[] one, byte[] two)
  4. mergeBytearrays(byte[] ret, byte[] header, byte[] body)
  5. mergeCharArrays(char[] array1, char[] array2)
  6. mergeNoDuplicates(String[] in1, String[] in2)
  7. mergeParameterVariableNameDescription(String[] parameterType, String[] variableName)
  8. mergerBy(String[] target, String seperator)
  9. mergeSort(Object[] src, Object[] dest, int low, int high, int off)