简体   繁体   中英

how to create dynamic two dimensional array in 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?

Since the number of columns is a constant, you can just have an 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.

There are no multi-dimensional arrays in Java, there are, however, arrays of arrays.

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>();

One more example for 2 dimension String array:

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

Try to make 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.

如何制作包含数组的自定义类,并使用自定义类的数组。

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

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.

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.
    }
 }
List<Integer>[] array;  
array = new List<Integer>[10]; 

this the second case in @TofuBeer's answer is incorrect. because can't create arrays with generics . u can use:

List<List<Integer>> array = new ArrayList<>();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM