tags:

views:

120

answers:

7

hello, can anyone tell me the error in this java declaration String[][] t=new String[15][15]; this works fine and if i use String[][] t=new String[][]; because i need to declare the variable t as dynamic as i am not sure how much values i am going to store in t.

+4  A: 

Use ArrayList (or other array object who can handle any number of objects). A java array always has a fixed length since some memory will be reserved for the array.

ArrayList creates such array as well to store the objects. Of you add more abjects as the current reserved size of the array than ArrayList will create a new bigger array (+50% if i'm correct). There are some other implementations that act a bit different (for example they create a new array 100% of the original one when the array is full. If performance is really important for you than you can look into this.

ArrayList<ArrayList<String> t = new ArrayList<ArrayList<String>();

void fill() {
    ArrayList<String> t2 = new ArrayList<String>();
    t2.add("somestring");
    String s = "someotherstring";
    t2.add(s);
    t.add(t2);
}
Mark Baijens
A Java array always has a FIXED length, not a maximum. Some of the entries may be null.
DJClayworth
your right, corrected it.
Mark Baijens
While this is possible, you need to create an ArrayList of (ArrayList of String), and `s = t[3][12];` becomes `s = t.get(3).get(12);`
Stephen P
can i add multidimensional in array list??? i have a key and a value i store key in array [0][0] and values in [0][1] like that one key has many values...
raju
If you have a key and a value you can use HashMap,`HashMap<String, String> hashMap = new HashMap<String, String>();`
Mark Baijens
If you're going to use the array for key/value pairs then Baijens answer above would work best imo.
ChadNC
+2  A: 

If you don't know how big it needs to be just declare it as

String[][] t;

and once you know how big it needs to be you can do (before trying to use the array)

t = new String[15][15];

If you're never sure how big the array need to be, you'll need to use something like a List of Lists.

List<List<String>> t = new ArrayList<List<String>>;

public void add(String str, int row, int col) {
    while (row >= t.size())
        t.add(new ArrayList<String>());

    List<String> row_list = t.get(row);
    while (col >= row_list.size())
        row_list.add("");

    row_list.set(col, str);
}
bemace
if i use `String[][] t= null; ` i am getting an error that Exception in thread "main" java.lang.NullPointerException while using it `t[0][0]="ana"`
raju
@raju : you still need to do `t = new String[sizeX][sizeY];` *after* you've discovered what the dimensions need to be but *before* your `t[0][0] =` assignment.
Stephen P
+1  A: 

In Java array objects are always of fixed length. Once you have allocated them you cannot change their size. An array variable can be made to point to different array objects of different size. So you can allocate:

String[][] t;

which doesn't point to an object and allocate an object later once you know the size:

int n1,n2;
// calculate n1,n2
t = new String[n1][n2];

If you need a structure where the size can change you are much better off using ArrayList, which can be dynamically resized.

DJClayworth
A: 

A Vector can grow too:

Vector<Vector<String>> t = new Vector<Vector<String>>();

Then call the Add method to add items to the vector.

baflgraf
Why Vector? It is needlessly synchronized.
Ishtar
A: 

As bemace said, just because you are declaring the variable doesn't mean you have to initialize it immediately.

As Mark said, arrays have a fixed size. Once an array is initialized(not declared, initialized) it has a fixed size.

So, there are two possibilities:

Either you will know how big the array needs to be before you need to start using it, in which case you can simply delay your initialization as suggested by bemace.

Or you won't know how big it needs to be before you start using it, in which case you need to use a dynamically sized data structure(s). Check out the Java Collections API:

tutorial api reference

Mike Edwards
A: 

declare it as String [][]t = null; and reinitialize it with actual length once you get it.

t=new String[x][y];

Nrj
A: 
Stephen P