Java List Create createMatrix(List source, List target)

Here you can find the source of createMatrix(List source, List target)

Description

Create matrix for the compare algorithms.

License

Apache License

Parameter

Parameter Description
source the source list.
target the target list.

Return

the matrix.

Declaration

private static int[][] createMatrix(List<String> source, List<String> target) 

Method Source Code

//package com.java2s;
/*//from  ww w. j a  v  a 2  s.  c  o  m
 * Copyright 2011 Andrej Petras <andrej@ajka-andrej.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.List;

public class Main {
    /**
     * Create matrix for the compare algorithms.
     * 
     * @param source the source list.
     * @param target the target list.
     * 
     * @return the matrix.
     */
    private static int[][] createMatrix(List<String> source, List<String> target) {
        int M = source.size();
        int N = target.size();

        int[][] result = new int[M + 1][N + 1];

        for (int i = M - 1; i >= 0; i--) {
            for (int j = N - 1; j >= 0; j--) {
                if (source.get(i).equals(target.get(j))) {
                    result[i][j] = result[i + 1][j + 1] + 1;
                } else {
                    result[i][j] = Math.max(result[i + 1][j], result[i][j + 1]);
                }
            }
        }
        return result;
    }
}

Related

  1. createListOfValidNumberOfPoints(int[] terms, int min, int max)
  2. createListWithArray(Object[] array)
  3. createListWithObjects(Object... objects)
  4. createMapFromList(List list)
  5. createMapOfObjects(List keys, List values)
  6. createMergedRegex(List regexes)
  7. createMultiParamMessage(String aPrefix, String aSuffix, List aParams)
  8. createMultiSelectionValue(List values)
  9. createMutableList(Collection collection)