如何在java中创建动态二维数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2707357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to create dynamic two dimensional array in java?
提问by Arivu2020
I want to create a two dimensional array dynamically.
我想动态创建一个二维数组。
I know the number of columns. But the number of rows are being changed dynamically. I tried the array list, but it stores the value in single dimension only. What can I do?
我知道列数。但是行数正在动态更改。我尝试了数组列表,但它仅将值存储在一维中。我能做什么?
采纳答案by polygenelubricants
Since the number of columns is a constant, you can just have an List
of int[]
.
由于列数是一个常数,因此您可以只使用List
of int[]
。
import java.util.*;
//...
List<int[]> rowList = new ArrayList<int[]>();
rowList.add(new int[] { 1, 2, 3 });
rowList.add(new int[] { 4, 5, 6 });
rowList.add(new int[] { 7, 8 });
for (int[] row : rowList) {
System.out.println("Row = " + Arrays.toString(row));
} // prints:
// Row = [1, 2, 3]
// Row = [4, 5, 6]
// Row = [7, 8]
System.out.println(rowList.get(1)[1]); // prints "5"
Since it's backed by a List
, the number of rows can grow and shrink dynamically. Each row is backed by an int[]
, which is static, but you said that the number of columns is fixed, so this is not a problem.
由于它由 a 支持List
,因此行数可以动态增长和收缩。每行都由一个 支持int[]
,它是静态的,但是您说列数是固定的,所以这不是问题。
回答by KM?n
How about making a custom class containing an array, and use the array of your custom class.
如何制作包含数组的自定义类,并使用自定义类的数组。
回答by TofuBeer
There are no multi-dimensional arrays in Java, there are, however, arrays of arrays.
Java 中没有多维数组,但是有数组的数组。
Just make an array of however large you want, then for each element make another array however large you want that one to be.
只需创建一个你想要的数组,然后为每个元素创建另一个你想要的数组。
int array[][];
array = new int[10][];
array[0] = new int[9];
array[1] = new int[8];
array[2] = new int[7];
array[3] = new int[6];
array[4] = new int[5];
array[5] = new int[4];
array[6] = new int[3];
array[7] = new int[2];
array[8] = new int[1];
array[9] = new int[0];
Alternatively:
或者:
List<Integer>[] array;
array = new List<Integer>[10];
// of you can do "new ArrayList<Integer>(the desired size);" for all of the following
array[0] = new ArrayList<Integer>();
array[1] = new ArrayList<Integer>();
array[2] = new ArrayList<Integer>();
array[3] = new ArrayList<Integer>();
array[4] = new ArrayList<Integer>();
array[5] = new ArrayList<Integer>();
array[6] = new ArrayList<Integer>();
array[7] = new ArrayList<Integer>();
array[8] = new ArrayList<Integer>();
array[9] = new ArrayList<Integer>();
回答by Wonson
Try to make Treemap < Integer, Treemap<Integer, obj> >
尝试制作 Treemap < Integer, Treemap<Integer, obj> >
In java, Treemap is sorted map. And the number of item in row and col wont screw the 2D-index you want to set. Then you can get a col-row table like structure.
在java中,Treemap是排序图。并且行和列中的项目数不会破坏您要设置的 2D 索引。然后你可以得到一个类似结构的列行表。
回答by shahnoor rahman
Here is a simple example.
this method will return a 2 dimensional tType
array
这是一个简单的例子。此方法将返回一个二维tType
数组
public tType[][] allocate(Class<tType> c,int row,int column){
tType [][] matrix = (tType[][]) Array.newInstance(c,row);
for (int i = 0; i < column; i++) {
matrix[i] = (tType[]) Array.newInstance(c,column);
}
return matrix;
}
say you want a 2 dimensional String array, then call this function as
说你想要一个二维字符串数组,然后调用这个函数作为
String [][] stringArray = allocate(String.class,3,3);
This will give you a two dimensional String array with 3 rows and 3 columns;
Note that in Class<tType> c
-> c
cannot be primitive type like say, int
or char
or double
. It must be non-primitive like, String
or Double
or Integer
and so on.
这将为您提供一个 3 行 3 列的二维 String 数组;请注意, in Class<tType> c
->c
不能是像 say, int
or char
or那样的原始类型double
。它必须是非原始的,例如,String
或Double
或Integer
等等。
回答by beetlej
One more example for 2 dimension String array:
二维字符串数组的另一个示例:
public void arrayExam() {
List<String[]> A = new ArrayList<String[]>();
A.add(new String[] {"Hyman","good"});
A.add(new String[] {"Mary","better"});
A.add(new String[] {"Kate","best"});
for (String[] row : A) {
Log.i(TAG,row[0] + "->" + row[1]);
}
}
Output:
输出:
17467 08-02 19:24:40.518 8456 8456 I MyExam : Hyman->good
17468 08-02 19:24:40.518 8456 8456 I MyExam : Mary->better
17469 08-02 19:24:40.518 8456 8456 I MyExam : Kate->best
回答by SHASHI VERMA
simple you want to inialize a 2d array and assign a size of array then a example is
很简单,你想初始化一个二维数组并分配一个数组的大小,那么一个例子是
public static void main(String args[])
{
char arr[][]; //arr is 2d array name
arr = new char[3][3];
}
//this is a way to inialize a 2d array in java....
回答by Ankit Jaiswal
Scanner sc=new Scanner(System.in) ;
int p[][] = new int[n][] ;
for(int i=0 ; i<n ; i++)
{
int m = sc.nextInt() ; //Taking input from user in JAVA.
p[i]=new int[m] ; //Allocating memory block of 'm' int size block.
for(int j=0 ; j<m ; j++)
{
p[i][j]=sc.nextInt(); //Initializing 2D array block.
}
}