如何在java中创建动态二维数组?

时间:2021-12-29 21:37:34

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

由于它由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<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>();

#3


2  

One more example for 2 dimension String array:

2维String数组的另一个例子:

public void arrayExam() {
    List<String[]> A = new ArrayList<String[]>();
    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<Integer, obj> >

尝试使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<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

假设您想要一个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<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数组;请注意,在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<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.
    }
 }

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

#1


35  

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

由于列数是常量,因此您只能拥有一个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.

由于它由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<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>();

#3


2  

One more example for 2 dimension String array:

2维String数组的另一个例子:

public void arrayExam() {
    List<String[]> A = new ArrayList<String[]>();
    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<Integer, obj> >

尝试使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<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

假设您想要一个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<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数组;请注意,在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<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.
    }
 }

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