How to convert a 1d array to 2d array?

2019-01-03 18:15发布

Say, I have a 1d array with 30 elements:

array1d[0] = 1  
array1d[1] = 2  
array1d[2] = 3  
.  
.  
.  
array1[29] = 30

How to convert the 1d array to 2d array?
Say 10x3?

array2d[0][0] = 1 array2d[0][1] =2 array2d[0][2] =3
.
.
.
array2d[9][0] = 28 array2d[9][1] =29 array2d[9][2] =30

Should I use a for loop?
But I cannot work it out.

8条回答
【Aperson】
2楼-- · 2019-01-03 18:37

This is just a pseudo code. ROWS represent the length of the 2d array, and COLUMNS represent the length of the 1st element in the array

for(i=0; i<ROWS; i++)
   for(j=0; j<COLUMNS; j++)
       array2d[i][j] = array1d[ (i*COLUMNS) + j];
查看更多
放荡不羁爱自由
3楼-- · 2019-01-03 18:42

You often will find the same problem: how to manipulate 2D array as 1D array. I wrote a generic class Grid, that lets access objects by index or by (x,y).

See the following class and understand the idea behind it. :)

You could use the following class for data manipulation as 2D array or 1D array. Here is the code for that I wrote and use.

/**
 * Grid represents a 2 dimensional grid.
 *
 * @param <E> the type of elements in this grid
 */

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

public class Grid<E>
{
    private int     size    ;
    private int     width   ;
    private int     height  ;
    private List<E> elements;

    public int getCapacity()
    {
        return getWidth() * getHeight();
    }

    /**
     * @return number of elements in grid. Null is also an element.
     */
    public int getSize()
    {
        return getElements().size();
    }

    /**
     * @param sideSize size of the grid side
     */
    public Grid(int sideSize)
    {
        this(sideSize,sideSize);
    }

    /**
     * @param width of the grid
     * @param height of the grid
     */
    public Grid(int width, int height)
    {
        this.width  = width ;
        this.height = height; 
        this.elements = new ArrayList<E>(
                Collections.nCopies(width*height, (E)null));
    }

    public int getHeight()
    {
        return height;
    }

    public int getWidth()
    {
        return width;
    }

    /**
     * @return all elements of the grid
     */
    public List<E> getElements()
    {
        return elements;
    }

    /**
     * @return iterator for a grid
     */
    public Iterator<E> iterator()
    {
        return getElements().iterator();
    }

    /**
     * Returns the element at position (x,y).
     *
     * @return the element at position (x,y)
     */
    public E get(int x, int y)
    {
        return getElements().get(
                idx(x,y));
    }

    /**
     * Returns the element at index idx.
     *
     * @return the element at given index
     */
    public E get(int idx)
    {
        return getElements().get(idx);
    }

    /**
     * Puts an element to the position idx
     *
     * @param element to be added
     *
     * @param x position x to add element to
     *
     * @param y position y to add element to
     */
    public void put(int x, int y, E element)
    {
        put(idx(x,y), element);
    }

    /**
     * Puts an element to the position idx
     *
     * @param element to be added
     *
     * @param idx to add element at
     */
    public void put(int idx, E element)
    {
        getElements().add(idx, element);
    }

    /**
     * Returns the x coordinate from the index.
     *
     * @return x coordinate of the index
     */
    public int x(int idx)
    {
        return idx % getHeight();
    }

    /**
     * Returns the y coordinate from the index.
     *
     * @return y coordinate of the index
     */
    public int y(int idx)
    {
        return (idx - idx % getHeight()) / getHeight();
    }

    /**
     * Returns index of element at (x,y).
     *
     * @return index of the coordinates
     */
    public int idx(int x, int y)
    {
        return y*getHeight() + x;
    }
}

Here is how to use the class (see a test example):

public class TestGrid
{
    public static final int     SIZE = 10;
    public static final Integer el1  = new Integer(2);
    public static final Integer el2  = new Integer(3);
    public static final Integer el3  = new Integer(3);

    public static void main(String[] args)
    {
        Grid<Integer> grid = new Grid<>(SIZE);
        assert grid.getCapacity() == SIZE*SIZE ;

        assert grid.idx(0,0) == 0 ;
        assert grid.idx(1,0) == 1 ;
        assert grid.idx(0,1) == 10;
        assert grid.idx(6,1) == 16; 
        assert grid.idx(9,9) == 99;

        grid.put(1, el1);
        assert grid.get(1) == el1 : grid.get(1);

        grid.put(0, 1, el2);
        assert grid.get(0,1) != el1 && el1 != el2 && grid.get(0,1) == el2;

        grid.put(15, el3);
        assert grid.get(5,1) == el3;
    }
}
查看更多
登录 后发表回答