4

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 System.out.println line. how do I print the values of the arraylist matrix?? can anyone suggest me a method??

vineeth
  • 641
  • 4
  • 11
  • 25
  • 1
    http://stackoverflow.com/questions/4401850/how-to-create-a-multidimensional-arraylist-in-java – Kumar Saurabh Sep 03 '15 at 12:56
  • 2
    You mean you don't get an error in `table[0][1].add(10)`? Considering that it has not been initialized, that would be strange. – RealSkeptic Sep 03 '15 at 12:57
  • @KumarSaurabh The declaration is correct. Only problem with `get()` method – Suresh Atta Sep 03 '15 at 12:58
  • @sᴜʀᴇsʜᴀᴛᴛᴀ Actually, Kumar may be on to the real problem here. It seems the OP thinks he has a 10x10 2D list of numbers, rather than a 10x10 array of lists of numbers (actually, raw lists...) – RealSkeptic Sep 03 '15 at 13:05
  • @RealSkeptic Nope. The indeces confusion. Added at one place and accessing another. Look at my answer. – Suresh Atta Sep 03 '15 at 13:08
  • @vineeth, which of [these two structures](http://i.imgur.com/Gs2nI7B.png) did you try to represent with your array? – RealSkeptic Sep 03 '15 at 13:26

4 Answers4

1

THe actual exception is at the lines

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

You are adding element at 0,0 position and trying to add the arraylist element at 0,1.

Hence the nullpointer.

Try

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)); 
    }
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • i tried using 1 and 0 but i am getting null pointer exception. I dont know how to get the value from table[1][0] – vineeth Sep 03 '15 at 13:02
  • @Suresh i tried using both indices and get(1).get(0) but it still gives null pointer exception – vineeth Sep 03 '15 at 13:18
  • @vineethPrabhakaran Wait.. look the lines `table[0][0] = new ArrayList(); table[0][0].add(10); ` and indeces. I changed your code and updated. Please run and see the difference. – Suresh Atta Sep 03 '15 at 13:21
1

For using ArrayList, you must first declare the type of ArrayList in angular braces. Try using the code snippet as below:

    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]

To insert a new row, you must do

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

and to append another element on a specific row you must do

table.get(row).add(someValue);
burglarhobbit
  • 1,860
  • 2
  • 17
  • 32
1

If you are looking for something more than toy project you should seriously consider using some external matrix library. Arrays will be painful in maintenance.

I could recommend EJML. With usage of this library your code will look like this:

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

Additionally it is quite likely that you will have to make some calculations inside your matrix. Library will have some support for basic ones, and will save you some time.

Marcin Szymczak
  • 11,199
  • 5
  • 55
  • 63
1

You seem to think you have a 2-D matrix of numbers, stored as an ArrayList. That is not what you have at all. Instead, you have a 2-D matrix where each element is an ArrayList. That means you really have 3 dimensions represented in your code. I don't think that's what you want. There are several ways you could achieve two dimensions using the constructs you already have (i.e. without going to some external library).

A 2-D array of numbers

The array is an easy to understand construct, so let's start with that.

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

This code declares a 2-D array of type Number, then initializes it with 10 rows and 10 columns. It then partially fills in numbers. As long as you access an element that has already been initialized, you'll be ok. Trying to access an element that hasn't yet been initialized (like table[3][4]) would be bad.

Another way to initialize an array

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

This is the same thing as before, but initialized all at once. This particular array only has 2 rows and 2 columns.

Nested ArrayLists

If you want to use an ArrayList instead of an array, that's fine. You just need to realize that the ArrayList actually will contain other ArrayLists, each of which will contain Numbers. Like so:

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

In this example, you first declare an ArrayList that contains ArrayLists that contain Numbers, and initialize the outer ArrayList. Then you create some inner ArrayLists, and finally give each of them some Numbers.

Summary

You can use arrays or ArrayLists as you prefer. You just have to initialize them correctly before accessing their elements. How to initialize depends on the data structure you choose.

All the codes

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();
  }
}
Erick G. Hagstrom
  • 4,873
  • 1
  • 24
  • 38