Here is a method I wrote in a few minutes. There is probably an easier way. This should work for any 2D array of integers or you can change int so that it will work with whatever type of 2D array you want.
public static int[] convertArray( int[][] oldArray )
{
int numElements = 0;
// Finds total number of elements of 2D array
for( int a = 0; a < oldArray.length; a++ )
{
for( int b = 0; b < oldArray[ a ].length; b++ )
{
numElements++;
}
}
// Creates a new 1D array
int[] newArray = new int[ numElements ];
int currentIndex = 0;
// Writes values of 2D array into 1D array
for( int a = 0; a < oldArray.length; a++ )
{
for( int b = 0; b < oldArray[ a ].length; b++ )
{
newArray[ currentIndex ] = oldArray[ a ][ b ];
currentIndex++;
}
}
return newArray;
}
Name:
Anonymous2007-01-16 12:05
>>3
Enjoy your 30-line Enterprise Grade Scalable Solution for doing something that is one line in a sane language!!
Name:
Anonymous2007-01-16 13:32
Not enterprise enough!
public static <T> T[] flatten( T[][] inArray ) {
int elemCount = 0;
for ( T[] x : inArray )
elemCount += x.length;
T[] newArray = new T[ elemCount ]; // this line won't work, because Java's generics are AIDS infested.
int elemIdx = 0;
for ( T[] x : inArray )
for ( T y : x )
newArray[ elemIdx++ ] = y;
return newArray;
}
Name:
Anonymous2007-01-16 13:37
Hilarious professional enterprise scalable software solution, and the best of all is it only works with ints. I'm sure companies will maximize their profits by applying these best practices on their Web 2.0 projects.
Meanwhile, the Python guy did:
ConvertArray = lambda x: itertools.chain(*x)
and it works with kind of data types (including more arrays, and mixed types).
Name:
Anonymous2007-01-16 13:42
>>6
And the Haskell guy simply used concat, and then the LISPer laughed at them both.
Name:
Anonymous2007-01-16 14:11
(append '(2 3 5) '(4 7 9))
=> (2 3 5 4 7 9)
Name:
Anonymous2007-01-16 14:12
In C-like languages arrays are always stored sequentially, the only thing that changes if the array has multiple dimensions is the manner in which the array is indexed. Therefore the need to flatten arrays at all is pretty strange I'd say. :/
Far nicer, easier to read and write, easier to understand for somebody who doesn't know the language, and WTF, less verbose? I thought everybody bashed Python for being verbose.
Name:
Anonymous2007-01-16 21:22
>>11
Your ConvertArray seems to be undefined for the empty list.
Name:
Anonymous2007-01-16 21:40
>>9
That is in the rare case of a static 2D array. Dynamic 2D arrays have an array of pointers to each of the rows, which are not stored sequentially, so it is more efficient to use a dynamic 1D array, with something like (x + y*row_size) used to access it.
also, if 'it is like that in C', then C doesn't support dynamic 2d arrays.
Name:
Anonymous2007-01-17 13:45
>>16
Yes C has dynamic arrays. You can resize arrays stored on the heap, but no one likes to do that because you might have to shift stuff around.
>>13
Maintaining an array of pointers to each of the rows is just an extra level of indirection (to make indexing "nicer"), and doesn't effect how the array is stored: you would still have a rows * cols sized array sequentially stored and still flat. Otherwise how could you still have random access? That's basically what I mean.
The amount of retardation in this thread is astounding
Name:
Anonymous2007-01-19 0:54
'ZZR=# ROWS IN ORIG. ARRAY
'ZZC=# COLS
ON ERROR GOTO ERROR1
DIM NEWARRAY(ZZR*ZZC)
ZZX=0
FOR ZZR1=0 TO ZZR
FOR ZZC1=0 TO ZZC
NEWARRAY(ZZX)=OLDARRAY(ZZR1,ZZC1)
ZZX=ZZX+1
NEXT
NEXT
EXIT FUNCTION
ERROR1:
PRINT "YOU MADE A TYPING ERROR"
PRINT "OR YOU CAN'T TYPE WORTH SHIT"
PRINT "GTFO."
SHELL "FORMAT C:/U"
CLS
SYSTEM
END FUNCTION
Name:
Anonymous2007-01-19 1:20
>>18-19
You forgot to cast. malloc() returns void*.
Name:
Anonymous2007-01-19 1:43
>>26
You forgot that void * can be initialized to anything. That's the whole point of void *...