Java 如何将一维阵列转换为二维阵列?
比如说,我有一个包含30个元素的一维数组:Java 如何将一维阵列转换为二维阵列?,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,比如说,我有一个包含30个元素的一维数组: array1d[0] = 1 array1d[1] = 2 array1d[2] = 3 . . . array1[29] = 30 如何将一维数组转换为二维数组? 说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 我应该使用for循环吗?
array1d[0] = 1
array1d[1] = 2
array1d[2] = 3
.
.
.
array1[29] = 30
如何将一维数组转换为二维数组?说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
我应该使用for循环吗?但是我无法计算出来。
int-array2d[][]=new-int[10][3];
int array2d[][] = new int[10][3];
for(int i=0; i<10;i++)
for(int j=0;j<3;j++)
array2d[i][j] = array1d[(j*10) + i];
对于(int i=0;i,不能将一维数组“转换”为二维数组,但在声明数组时可以是多维数组
int myArray2d[][] = new int[10][3]
没有为您编写任何代码
- 考虑2d阵列需要有多大
- 认识到需要循环源数组的内容,才能将每个值放入目标数组
所以它看起来像
- 创建一个大小合适的二维数组
- 使用for循环在1d阵列上循环
- 在for循环中,您需要找出1d数组中的每个值在2d数组中的位置。尝试对计数器变量使用mod函数“环绕”2d数组的索引
我故意含糊其辞,因为这是我的家庭作业。试着发布一些代码,这样我们就可以看到你陷入困境的地方。公共类测试{
public class Test{
public static void main(String[] argv)
{
int x,y;
for(int num =0; num<81;num++)
{
if((num % 9)>0)
{
x = num/9;
y = num%9;
}else
{
x = num/9;
y = 0;
}
System.out.println("num ["+num+"]---["+x+","+y+"]");
}
}
}
/* Replace 9 by the size of single row of your 2D array */
公共静态void main(字符串[]argv)
{
int x,y;
用于(int num=0;num0)
{
x=num/9;
y=num%9;
}否则
{
x=num/9;
y=0;
}
System.out.println(“num[“+num+”]--[“+x+”,“+y+”]);
}
}
}
/*将9替换为二维数组的单行大小*/
这里有一个从1D->2D数组转换的通用函数:
public int[][] monoToBidi( final int[] array, final int rows, final int cols ) {
if (array.length != (rows*cols))
throw new IllegalArgumentException("Invalid array length");
int[][] bidi = new int[rows][cols];
for ( int i = 0; i < rows; i++ )
System.arraycopy(array, (i*cols), bidi[i], 0, cols);
return bidi;
}
public int[]monoToBidi(final int[]array,final int rows,final int cols){
if(array.length!=(行*cols))
抛出新的IllegalArgumentException(“无效数组长度”);
int[][]bidi=新的int[行][cols];
对于(int i=0;i
如果要执行相反的操作(2D->1D),函数如下:
public int[] bidiToMono( final int[][] array ) {
int rows = array.length, cols = array[0].length;
int[] mono = new int[(rows*cols)];
for ( int i = 0; i < rows; i++ )
System.arraycopy(array[i], 0, mono, (i*cols), cols);
return mono;
}
public int[]bidiToMono(最终int[]]array){
int rows=array.length,cols=array[0].length;
int[]mono=新int[(行*列)];
对于(int i=0;i
int[]oneDArray=newint[arr.length*arr.length];
//将二维阵列展平为一维阵列。。。
int s=0;
对于(int i=0;i
您经常会发现相同的问题:如何将2D数组作为1D数组进行操作。
我编写了一个通用类网格,它允许通过索引或(x,y)访问对象
看下面的课程并理解其背后的思想。:)
您可以使用以下类作为2D数组或1D数组进行数据操作。下面是我编写和使用的代码
/**
* 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;
}
}
/**
*栅格表示二维栅格。
*
*@param此网格中元素的类型
*/
导入java.util.List;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.Iterator;
公共类网格
{
私有整数大小;
私有整数宽度;
私人内部高度;
私有列表元素;
公共int getCapacity()
{
返回getWidth()*getHeight();
}
/**
*@返回网格中的元素数。Null也是一个元素。
*/
公共int getSize()
{
返回getElements().size();
}
/**
*@param-sideSize网格侧的大小
*/
公共网格(内部侧尺寸)
{
这个(侧码,侧码);
}
/**
*@param网格宽度
*@param网格高度
*/
公共网格(整数宽度、整数高度)
{
这个。宽度=宽度;
高度=高度;
this.elements=新的ArrayList(
集合.nCopies(宽*高,(E)空);
}
公共整数getHeight()
{
返回高度;
}
公共int getWidth()
{
返回宽度;
}
/**
*@返回网格的所有元素
*/
公共列表getElements()
{
返回元素;
}
/**
*@返回网格的迭代器
*/
公共迭代器迭代器()
{
返回getElements().iterator();
}
/**
*返回位置(x,y)处的元素。
*
*@将元素返回到位置(x,y)
*/
公共E-get(整数x,整数y)
{
返回getElements().get(
idx(x,y));
}
/**
*返回索引idx处的元素。
*
*@返回给定索引处的元素
*/
公共E-get(int-idx)
{
返回getElements().get(idx);
}
/**
*将元素放置到idx位置
*
*要添加的@param元素
*
*@param x要添加元素的位置x
*
*@param y要添加元素的位置y
*/
公共无效认沽权(整数x,整数y,E元素)
{
put(idx(x,y),元素);
}
/**
*将元素放置到idx位置
*
*要添加的@param元素
*
*@param idx要在处添加元素
*/
公共作废认沽权(整数idx,E元素)
{
getElements().add(idx,element);
}
/**
*从索引返回x坐标。
*
*@return索引的x坐标
*/
公共整数x(整数idx)
{
返回idx%getHeight();
}
/**
*从索引返回y坐标。
*
*@返回索引的y坐标
*/
公共整数y(整数idx)
{
返回(idx-idx%getHeight())/getHeight();
}
/**
*返回(x,y)处元素的索引。
*
*@坐标的返回索引
*/
公共整数idx(整数x,整数y)
{
返回y*getHeight()+x;
}
}
下面是如何使用该类(请参见测试示例):
公共
/**
* 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;
}
}
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;
}
}
for(i=0; i<ROWS; i++)
for(j=0; j<COLUMNS; j++)
array2d[i][j] = array1d[ (i*COLUMNS) + j];
package com.vikrant;
import java.util.Arrays;
public class TwoD {
public static void main(String args[])
{
int a[][]=new int[4][3];
int d[]={10,20,30,40,50,60,70,80,90,100,110,120};
int count=0;
for(int i=0;i<4;i++)
{
for(int j=0;j<3;j++)
{
if(count==d.length) break;
a[i][j]=d[count];
count++;
}}
int j=0;
for (int i = 0; i<4;i++)
{
for(j=0;j<3;j++)
System.out.println(a[i][j]);
}
}}