Java新建窗体DynamicArray,如何在java中創建動態二維數組?

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?

我知道列數。但行數正在動態更改。我嘗試了數組列表,但它僅將值存儲在單維度中。我能做什么?

8 个解决方案

#1

35

Since the number of columns is a constant, you can just have an List of int[].

由於列數是常量,因此您只能擁有一個int []列表。

import java.util.*;

//...

List rowList = new ArrayList();

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.

由於它由List支持,因此行數可以動態增長和縮小。每一行都由一個int []支持,這是靜態的,但你說列數是固定的,所以這不是問題。

#2

15

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[] array;

array = new List[10];

// of you can do "new ArrayList(the desired size);" for all of the following

array[0] = new ArrayList();

array[1] = new ArrayList();

array[2] = new ArrayList();

array[3] = new ArrayList();

array[4] = new ArrayList();

array[5] = new ArrayList();

array[6] = new ArrayList();

array[7] = new ArrayList();

array[8] = new ArrayList();

array[9] = new ArrayList();

#3

2

One more example for 2 dimension String array:

2維String數組的另一個例子:

public void arrayExam() {

List A = new ArrayList();

A.add(new String[] {"Jack","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 : Jack->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

#4

1

How about making a custom class containing an array, and use the array of your custom class.

如何創建包含數組的自定義類,並使用自定義類的數組。

#5

1

Try to make Treemap < Integer, Treemap >

嘗試使Treemap

>

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索引。然后你可以獲得像結構一樣的col-row表。

#6

1

Here is a simple example. this method will return a 2 dimensional tType array

這是一個簡單的例子。此方法將返回2維tType數組

public tType[][] allocate(Class 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

假設您想要一個2維String數組,那么將此函數稱為

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 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數組;請注意,在Class

c - > c中,不能是諸如say,int或char或double之類的基本類型。它必須是非原始的,如String或Double或Integer等。

#7

0

Scanner sc=new Scanner(System.in) ;

int p[][] = new int[n][] ;

for(int i=0 ; 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

{

p[i][j]=sc.nextInt(); //Initializing 2D array block.

}

}

#8

-1

simple you want to inialize a 2d array and assign a size of array then a example is

很簡單你想要一個2d數組並分配一個數組的大小然后一個例子是

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....

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值