views:

142

answers:

3

I have a variable number of two-dimensional arrays. The first dimension is variable, the second dimension is constant. i.e.:

Object[][] array0 = {
{"tim", "sanchez", 34, 175.5, "bla", "blub", "[email protected]"},
{"alice", "smith", 42, 160.0, "la", "bub", "[email protected]"},
...
};

Object[][] array1 = {
{"john", "sdfs", 34, 15.5, "a", "bl", "[email protected]"},
{"joe", "smith", 42, 16.0, "a", "bub", "[email protected]"},
...
};

...

Object[][] arrayi = ...

I'm generating these arrays with a for-loop:

 for (int i = 0; i < filter.length; i++) {
  MyClass c = new MyClass(filter[i]);
  //data = c.getData();
 }

Where "filter" is another array which is filled with information that tells "MyClass" how to fill the arrays. "getData()" gives back one of the i number of arrays.

Now I just need to have everything in one big two dimensional array. i.e.:

Object[][] arrayComplete = {
{"tim", "sanchez", 34, 175.5, "bla", "blub", "[email protected]"},
{"alice", "smith", 42, 160.0, "la", "bub", "[email protected]"},
...
{"john", "sdfs", 34, 15.5, "a", "bl", "[email protected]"},
{"joe", "smith", 42, 16.0, "a", "bub", "[email protected]"},
...
...
};

In the end, I need a 2D array to feed my Swing TableModel.

Any idea on how to accomplish this? It's blowing my mind right now.

+7  A: 

Your head blowage will be significantly reduced if you start thinking in terms of objects and collections instead of arrays. Seriously, working with arrays like this gets way too complex really fast! This is really why OO programming was invented.

Start out with the simplest object: User. It looks like user contains first and last name, age, other stuff, and email address.

Now is all you have a collection of users? Trivial to manage!

Then you have to map that set of users to the swing table model. A simple toArray() method could return the one-dimensional array you want. Creating a 2-dimensional array from those should be easy, etc. (although I'm not sure a 2-dimensional array is the way to go)

Also, create a Person constructor that takes Object[] so that you don't have to change the way your objects are created--creating them as arrays the way you do is actually pretty efficient.

If it's still confusing after that, we should figure out why you are feeding your table model with an array--is that really necessary? Generally a collection of objects would be much easier to manage.

(Sorry about the multiple edits. Somehow I always notice more after hitting save...)

Bill K
Thanks for your help, but I don't get what you're saying.I also don't create my "persons", I retrieve them from a webpage.My program is far more complicated than this example. I tried to breake it down as simple as possible and now I'm having troubles applying your solution to my actual program.I guess what you're saying is that I have some major structual errors, but I'm note sure how to manage it correctly.
qlb
My Person-Object is responsible for retrieving the data from a webpage. The "MyClass" contructor takes this Person-Object and puts the information in jTableModel conform format. But now I have multiple Person objects.Sorry, maybe my example was a little wrong, as I broke it down to the problem of adding a bunch of 2d arrays together to one big 2d array.
qlb
The optimal way would be to implement your own JTableModel and store a collection of "Person" objects. When a request comes into your table model, grab the correctly numbered Person from your collection, then return the attribute from that object that matches the column requested. The JTableModel object becomes your translation (controller) between your model (Person) and view (JTable).
Bill K
+2  A: 

I understand that Swing table uses array, but it doesn't mean you should too!

Do what Bill describes and break your data down into object classes and then feed it into your table one row at a time. This will keep your data representation nice.

Swing is for "displaying" and "input" ONLY. It should not be where you store the data nor how you represent the data.

Pyrolistical
+1  A: 

You should listen to the advice others are giving, but if you insist on doing what you originally intend to do, this is how:

public static Object[][] concat(Object[][]... arrays) {
    int length = 0;
    for (Object[][] comp : arrays) {
        length += comp.length;
    }
    Object[][] ret = new Object[length][];
    int offset = 0;
    for (Object[][] comp : arrays) {
        System.arraycopy(comp, 0, ret, offset, comp.length);
        offset += comp.length;
    }
    return ret;
}

So now for example, you can do the following:

    Object[][] arr1 = {
        { "A" },
        { "B" },
    };
    Object[][] arr2 = {
        { "1" },
        { "2" },
    };
    Object[][] arr3 = {
        { "x", "x" },
        { "y", "y" },
    };
    Object[][] all = concat(arr1, arr2, arr3);
    System.out.println(Arrays.deepToString(all));
    // prints "[[A], [B], [1], [2], [x, x], [y, y]]"

You can also use it like this:

    Object[][] all = new Object[0][];
    all = concat(all, arr1);
    all = concat(all, arr2);
    all = concat(all, arr3);
    // :
    // maybe more... like in a loop

    System.out.println(Arrays.deepToString(all));
    // prints "[[A], [B], [1], [2], [x, x], [y, y]]"
polygenelubricants