views:

42

answers:

2

Hello, I was wonder how I should record this information:

#  num:  material:   type:  geo:  light:   tex:  extralight:  verts:
     0:        13  0x0000     4       3      0       0.0000       3    0,  0   1,  1   2,  2 
     1:        13  0x0000     4       3      0       0.0000       3    2,  2   3,  3   4,  4 
     2:        13  0x0000     4       3      0       0.0000       3    4,  4   5,  5   0,  0 
     3:        13  0x0000     4       3      0       0.0000       3    4,  4   0,  0   2,  2 
     4:        13  0x0000     4       3      0       0.0000       4    7,  7  12, 12   8,  8   0,  0  

I want to record it in a float[], but I cannot do it without having to know the size of it ahead of time(Which is impossible unless I run through it a second time). The reason I say this is that the range of values changes based on the value in verts. The number to the right of verts is the locations of Vertices; so if it's 4, there are 8 extra numbers, if it's 3, there are 6 extra numbers.

Side-note: Ignore the column "num:" as I'm not going to use any of those values.

+3  A: 

Use a dynamically expansible collection like a List. You don't need to know the size beforehand during construction. Instead of float[], use a List<Float> and use the add() method to add items.

See also:

BalusC
So, don't use a float for kind of stuff? Or do use one, but as the end product(As in, go from List to float array)? If the former, could you give me a quick example, as I've tried some of the collections .toArray, but I've been having trouble with it.
Unrealomega
You cannot use primitives in a collection. The `toArray()` can only produce a `Float[]`. To go from `List<Float>` to `float[]` you need to create a `new float[list.size()]`, loop over the list and fill the array. As an alternative you can use [`System#arraycopy()`](http://download.oracle.com/javase/6/docs/api/java/lang/System.html#arraycopy%28java.lang.Object,%20int,%20java.lang.Object,%20int,%20int%29) to create an expanded copy of the array during adding.
BalusC
Thanks. I don't remember how or if I can use Lists with multi-dimensional data like this, though I did read you could do List<List<Float>>, but it's not recommended.
Unrealomega
You want a dynamically expansible array. It's either using `List` or hassling with `System#arraycopy()` yourself (which `ArrayList` itself is doing under the hoods as well).
BalusC
A: 

The most obvious solution to me is to create an object.

class Information
{
     private final int material ;

     private final int type ;

     private final int geo ;

     private final int light ;

     private final int tex ;

     private final int extralight ;

     private final List<Float> verts ;

    // appropriate constructor

    // appropriate accessor methods
}
emory
Hmm, looks like it would work fine. :3 Thanks.
Unrealomega