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", "tim@tim.com"},
{"alice", "smith", 42, 160.0, "la", "bub", "alice@sdf.com"},
...
};
Object[][] array1 = {
{"john", "sdfs", 34, 15.5, "a", "bl", "john@tim.com"},
{"joe", "smith", 42, 16.0, "a", "bub", "joe@sdddf.com"},
...
};
...
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", "tim@tim.com"},
{"开发者_JAVA技巧alice", "smith", 42, 160.0, "la", "bub", "alice@sdf.com"},
...
{"john", "sdfs", 34, 15.5, "a", "bl", "john@tim.com"},
{"joe", "smith", 42, 16.0, "a", "bub", "joe@sdddf.com"},
...
...
};
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.
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...)
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.
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]]"
精彩评论