Reputation: 641
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??
Upvotes: 4
Views: 7067
Reputation: 4945
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).
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.
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.
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
.
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.
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();
}
}
Upvotes: 1
Reputation: 11443
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.
Upvotes: 1
Reputation: 2291
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);
Upvotes: 1
Reputation: 122018
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));
}
Upvotes: 1