Printing 2D ArrayList Matrix Values

I have a 2D Arraylist matrix like

    ArrayList[][] table = new ArrayList[10][10];
    table[0][0] = new ArrayList(); 
    table[0][1].add(10); 
    table[1][0].add(20); 
    table[1][1].add(30); 

    System.out.println("Value="+table[1][0].get()); //error line

The error occurs in the line System.out.println. how to print array matrix values? can anyone suggest me a method?

+4
source share
4 answers

, , , ArrayList. , . , ArrayList. , 3 , . , . (.. - ).

2-

- , .

Number[][] table = new Number[10][10];
table[0][0] = 0; 
table[0][1] = 10; 
table[1][0] = 20; 
table[1][1] = 30; 
System.out.println("Value="+table[1][0].get());

Number, 10 10 . . , , . , (, table[3][4]), .

Number[][] table = { { 0, 10 }, { 20, 30 } };
System.out.println("Value=" + table[1][0]);

, , . 2 2 .

ArrayList , . , ArrayList ArrayLists, Numbers. :

ArrayList<ArrayList<Number>> table = new ArrayList<>();
table.add(new ArrayList<>());
table.add(new ArrayList<>());
table.get(0).add(0);
table.get(0).add(10);
table.get(1).add(20);
table.get(1).add(30);
System.out.println("Value=" + table.get(1).get(0));

ArrayList, ArrayLists, Numbers, ArrayList. ArrayLists , , Numbers.

ArrayLists . , . .

import java.util.ArrayList;
public class TwoD {
  public void example1() {
    Number[][] table = new Number[10][10];
    table[0][0] = 0;
    table[0][1] = 10;
    table[1][0] = 20;
    table[1][1] = 30;

    System.out.println("\nExample 1");
    System.out.println("Value=" + table[1][0]);
  }

  public void example2() {
    Number[][] table = { { 0, 10 }, { 20, 30 } };

    System.out.println("\nExample 2");
    System.out.println("Value=" + table[1][0]);
  }

  public void example3() {
    ArrayList<ArrayList<Number>> table = new ArrayList<>();
    table.add(new ArrayList<>());
    table.add(new ArrayList<>());
    table.get(0).add(0);
    table.get(0).add(10);
    table.get(1).add(20);
    table.get(1).add(30);

    System.out.println("\nExample 3");
    System.out.println("Value=" + table.get(1).get(0));
  }

  public static void main(String[] args) {
    TwoD me = new TwoD();
    me.example1();
    me.example2();
    me.example3();
  }
}
+1

table[0][0] = new ArrayList(); 
  table[0][1].add(10); 

0,0 arraylist 0,1.

, nullpointer.

public static void main(String[] args) {
        ArrayList[][] table = new ArrayList[10][10];
        table[0][0] = new ArrayList(); 
        table[0][0].add(10); 
        table[0][0].add(20); 
        table[0][0].add(30); 

        System.out.println("Value="+table[0][0].get(1)); 
    }
+1

ArrayList ArrayList angular . , :

    ArrayList<ArrayList<Integer>> table = new ArrayList<ArrayList<Integer>>(); //2d ArrayList
    ArrayList<Integer> x = new ArrayList<Integer>(); 
    x.add(10);
    x.add(20);
    table.add(x); 
    table.add(x); 

    System.out.println("Value="+table); //Prints table[][]
    System.out.println("Value="+table.get(0)); //Prints table[0]
    System.out.println("Value="+table.get(0).get(1)); //Prints table [0][1]

,

table.add(new ArrayList<Integer>());

,

table.get(row).add(someValue);
+1

, , , . .

EJML. :

BlockMatrix64F matrix = new BlockMatrix64F(10, 10); 
matrix.set(0,1,10);
matrix.set(1,0,20);
matrix.set(1,1,30);

System.out.println("Value="+matrix.get(1,0));

, , . , .

+1

Source: https://habr.com/ru/post/1605738/


All Articles