Java Array Flatten flattenIndices(int[] indices, int[] extents)

Here you can find the source of flattenIndices(int[] indices, int[] extents)

Description

Given n-dimensional indices and their dimensional extents flatten them into a 1d index.

License

Open Source License

Parameter

Parameter Description
indices n-dimensional indices
extents the maximum size in each dimension

Return

Converts n-dimensional indices into a 1-dimension index

Declaration

public static int flattenIndices(int[] indices, int[] extents) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//  w w w .ja  v a2  s .co m
     * Given n-dimensional indices and their dimensional extents flatten them into a 1d index.
     * More detail: http://stackoverflow.com/questions/7367770/how-to-flatten-or-index-3d-array-in-1d-array
     *
     * @param indices n-dimensional indices
     * @param extents the maximum size in each dimension
     * @return Converts n-dimensional indices into a 1-dimension index
     */
    public static int flattenIndices(int[] indices, int[] extents) {
        int totalIdx = 0;
        for (int i = 0; i < indices.length; i++) {
            int idx = indices[i];
            for (int j = 0; j < i; j++) {
                int extent = extents[j];
                idx *= extent;
            }
            totalIdx += idx;
        }
        return totalIdx;
    }
}

Related

  1. flattenArguments(String[] arguments)
  2. flattenArray(Object... objects)
  3. flattenArray(Object[] words)
  4. flattenArray(String[] aString)
  5. flattenBytes(byte[] b)
  6. flattenNTriples(final String[] values)
  7. flattenStringArray(String[] s_array, String delimiter)